Dynamic Cross-Domain Canonical URL’s with PHP

Google announced support for specifying a canonical representation of content back in Feb of 2009. This was done in order to help webmasters avoid duplicate content in their sites by specifying the rel=”canonical” link element in the head section of duplicate pages. In December of 2009, Google announced the ability to implement the canonical tag cross-domain. This is particularly helpful for webmasters who have mulitple domain names pointing at the same content and cannot implement 301 redirects on their servers.

If you have a dynamic site, editing the head of each document individually is not possible. In thisĀ  situation you can utilize the Apache REQUEST_URI server variable in PHP to create the canonical link element dynamically. The example code is below:

<link rel="canonical" href="http://www.domain.com<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" />

In this example, you would put the code snippet in the head area of your dynamic page(s) on your other domains ie. domain.net and domain.org. This tells the search engines that no matter what the path or page is on those domains, the canonical location of that page is located on domain.com.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>