PHP script to generate the rel=canonical tag

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.
Google recognizes the rel=canonical directive between two different domains to solve problems of duplicate content, this since December 15, 2009.
It became in June 2012 an IETF standard known as RFC 6596.

It is possible that this tag solves a problem to a small fraction of webmasters, but it is more a concern for the majority.
Indeed, search engines do not always discern when a page is copied, which is the original and what is the copy. And if the copier add a canonical tag to its copy?
Wordpress users have an advantage in this game since the tag is included by default, unlike static sites.

So 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:

https://www.example.com/mypage.php?node=mykey 

Access through a category page:

https://www.example.com/mypage.php?node=mykey&category=mycat 

Access with a session identifier:

https://www.example.com/mypage.php?node=mykey&sessionid=1234 

Access by a number:

https://www.example.com/?1234 

Customized URL for SEO:

https://www.example.com/keyword1-keyword2    

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.example.com/keyword1-keyword2" />

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="https://www.scriptol.com/scripts/canonical.php" />

It can be produced automatically with this PHP code:

<link rel="canonical" href="https://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:s//www.example.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.

Link: Common mistakes with the canonical tag.