23 February, 2009 #web-design

PHP Canonical URL Script

On a recent project we had multiple duplicates of product detail pages mainly due to querystrings setting the sizes of the products. To combat this I decided to use the new Canonical REL URL tag now supported by Google, Yahoo and MSN and thought that if you’re experiencing the same issue that you’d appreciate this elegant solution to your own canonical url problems.

In the head just enter:

<link rel=”canonical” href=”http://www.website.com<?php echo $_SERVER[‘PHP_SELF’]?>”>

if you’re using a rewritten path then it needs to be changed to:

<link rel=”canonical” href=”http://www.website.com<?php echo parse_url($_SERVER[‘REQUEST_URI’],PHP_URL_PATH);?>“>

What this does is write in the url without the querystring in essence telling Google and the others that the non querystringed page is the one they should really index (googles supposed to 301 the links across).

0 Comments on PHP Canonical URL Script

Leave a Comment

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

You Might Be Interested In

Share via
Copy link
Powered by Social Snap