Fix Canonical URL with link rel=”canonical” in PHP

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

Recently the major search engines have worked together to help fix one of the biggest problems in SEO, The canonical URL. With this quick fix all you need to do is add a tag to the <head> section of the page and search engines will use this as an anchor to index the page.

since it is still fairly new we don’t know how it will work in the long run and if there are any side effects such as the pagerank not transferring.

The new tag looks like this

<link rel="canonical" href="">

But nothing can be that easy, obviously with a large site you wouldn’t want to write that out for a million pages so here is the PHP code to make it work.

$thispage = "http://www.";
$thispage .= $_SERVER["SERVER_NAME"];
$thispage .= $_SERVER["PHP_SELF"];
if($_SERVER["QUERY_STRING"]){$thispage .= "?".$_SERVER["QUERY_STRING"];}

<link rel="canonical" href="<? echo $thispage;?>">

Posted by Adi on February 15, 2009

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

Leave a Reply

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