WordPress.org

Ready to get started?Download WordPress

Forums

All in One SEO Pack
Canonical tag and daily content (1 post)

  1. ppostma1
    Member
    Posted 2 years ago #

    Our home page features an article every day. I'm noticing that the canonical by default declares the canonical href to be the current URL. That is exactly what we do not want to happen on the home page, declaring the canonical URL to be '/' when the content is pulled from /2012/05/21/xxxx

    In fact, in SEO if the canonical is the current URL, canonical is not supposed to be used. A page declaring itself to be itself is cliche

    I have this fix code:

    add_filter('aioseop_canonical_url', 'handle_canonical', 2, 1);
    function handle_canonical($url) {
    
      $uri = $_SERVER['REQUEST_URI'];
      if($i = strpos($uri, '?')){
        $uri = substr($uri, 0, $i);
      }
    
      if($uri == '/'){
        //actual canonical will be in the first fetched post
        if($GLOBALS['posts'] && $GLOBALS['posts'][0]){
          $main_post = $GLOBALS['posts'][0];
          $url = $main_post->guid;
          //got it, but chop it
          if(preg_match('~(https?://)?([a-z0-9\._-]+)?(/.*)~', $url, $m)){
            if($m[2] == $_SERVER['HTTP_HOST']){
              $url = $m[3];
            } else
            if(!empty($m[2])){
              $url = $m[2] . '/' . $m[3];
            } else {
              $url = $m[3];
            }
            $url = str_replace('//', '/', $url);
          }
        }
      }
    
      return $url;
    }

    to explain, if the current URL is the home page ($uri == '/' should probably be something like $uri == $site_root) then what is the main post's expected URL, where the content will permanently reside. It works cross domain while repairing an issue with our content importer defining the guid to be http://domain//uri/

    Should something like that be in the plugin, or should I place it in our template functions to remain at the template/content level?

    But please make sure the canonical tag does not print if the canonical href url equals the current URI

    http://wordpress.org/extend/plugins/all-in-one-seo-pack/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic