Your open source for Development and Design

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

Filed under: SEO — Tags: , , , , , — Adi @ 2:32 pm February 15, 2009

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="http://www.open-source-web.com/">

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;?>">

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Switch to our mobile site