Generic rel=canonical in PHP
Since December 15, 2009, Google recognizes the rel=canonical directive between two different domains to solve problems of duplicate content, on different domains for the same author. The canonical tag indicates what is the URL to be indexed when multiple URLs link on the same page or content even moved to a new page.
It is possible that this change solves a problem to a small fraction of webmasters, but it is more a concern for the majority.
Because if Google can usually know when a site copies the contents of another site without permission, it does not mean it is infallible.
And at the canonical game, Wordpress users, for example, take an advantage because the tag is included by default, while static sites or sites using other CMS can be the loser.
To restore the balance, you should add the tag in all your web pages, and to avoid tedious editing work, a few lines of PHP can be automatically included in your page template ...
Duplicate content
Duplicate content occurs inadvertently and not deliberately when a Web page is accessible to robots of search engines by two different URLs.
This is especially the case with CMS who can access the pages with different options, as outlined in the article on the Google's blog:
Access through the home page:
http://www.scriptol.com/mypage.php?node=mykey
Access through a category page:
http://www.scriptol.com/mypage.php?node=mykey&category=mycat
Access with a session identifier:
http://www.scriptol.com/mypage.php?node=mykey&sessionid=1234
Access by a number:
http://www.scriptol.com/?1234
Customized URL for SEO:
http://www.scriptol.com/key1-key2-key3
The disadvantages are many. First, PageRank coming form backlinks will be distributed among the different URLs, and on the other hand, search enbines will choose a unique URL to their indexes, but not necessarily the one the webmaster wants to put forward!
To resolve this problem, Google, Yahoo and Live Search together propose a new meta tag to be inserted into the HEAD section of the page.
<link rel="canonical" href="http://www.scriptol.com/key1-key2-key3" />
This may be the webmaster which will insert it, or a CMS, which will automatically generate the meta tag ... in a future version.
Source Google Webmaster Central. The same announcement was made on the blogs of Yahoo and Microsoft.
The canonical tag
It is located in the <head> section and has the form <link rel="canonical" href="url of page" />
For example, the link on this page is:
<link rel="canonical" href="http://www.scriptol.com/scripts/canonical.php" />
It can be produced automatically with this PHP code:
<link rel="canonical" href="http://www.scriptol.com<?php echo $_SERVER['PHP_SELF']; ?>" />
Replace of course http://www.scriptol.fr by the domain of your site.
Generic code
If your server is not configured to recognize the PHP_SELF variable, you can also try this code, a little longer:
<link rel="canonical" href="http://www.scriptol.com <?php echo substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT'])); ?>" />
Thus the same code can be used and placed automatically on all your pages.
Actually, it is possible to make the domain generic too, with the $_SERVER['HTTP_HOST'] variable, but if your site can be accessed with or without www, it must be avoided.
| Tweet |
|