Home arrow PHP arrow Page 4 - Using the Link Rel Canonical Tag

Implementing a Canonical Version, First Scenario - PHP

The link rel canonical tag lets you solve canonical issues for your URL without having to resort to 301 redirects or other potentially complicated approaches. This article explains how to take advantage of the tag in your PHP-powered web sites.

TABLE OF CONTENTS:
  1. Using the Link Rel Canonical Tag
  2. Theory of Implementation
  3. Implementation Steps
  4. Implementing a Canonical Version, First Scenario
  5. Implementing Canonical Version for a Secure Website
By: Codex-M
Rating: starstarstarstarstar / 6
May 07, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

This scenario focuses on a domain using a non-secure protocol (http://) and www as the canonical version of the site. Using the steps explained earlier, the resulting script should be:

<?php

//place this script between the <head> and </head> section of your header.php or related dynamic website template

//such as index.php, product_info.php in the OsCommerce templates

//this script is applicable when the CANONICAL PROTOCOL IS HTTP AND USING WWW VERSION.

//this script is NOT APPLICABLE to a subdomain of a main domain.

//Example: if your canonical version is www.mysite.com, you should NOT be using the script in any of its subdomain.

//First step eliminate any session IDs in the URL:

$requestedurl = $_SERVER["REQUEST_URI"];

//Define array of most common open source session IDs

$id=array('osCsid','zenid','PHPSESSID');

if (preg_match("/osCsid/i", $requestedurl))

{

$x=0;

}

elseif (preg_match("/zenid/i", $requestedurl))

{

$x=1;

}

elseif (preg_match("/PHPSESSID/i", $requestedurl))

{

$x=2;

}

if ((preg_match("/osCsid/i", $requestedurl)) || (preg_match("/zenid/i", $requestedurl)) || (preg_match("/PHPSESSID/i", $requestedurl)) )

{

//URL is session ID based

$position=(strpos($requestedurl,$id[$x]))- 1;

}

else

//no session ID

{

$position=strlen($requestedurl);

}

//trim the URLs any session ID

$cleanrequest=substr($requestedurl,0,$position);

//set protocol to http:// since this the canonical protocol

$protocol='http://';

//check if the server name contains www

if (preg_match("/www/i", $_SERVER["SERVER_NAME"]))

{

//the URL is using the www version

//display the complete canonical URL without any session ID

$canonical=$protocol.$_SERVER["SERVER_NAME"].$cleanrequest;

}

else

{

//append the canonical www version to the server name and display the canonical www version

$URL='www.'.$_SERVER["SERVER_NAME"];

$canonical=$protocol.$URL.$cleanrequest;

}

//Final step defining the final link rel canonical element

echo '<link rel="canonical" href="'.$canonical.'" />';

?>

The good thing about this script is that it will automatically remove the session IDs of the most common e-commerce templates, such as OsCommerce, Zen Cart and CRE loaded.

The resulting URL is SEO-friendly without the session IDs in it, while using the http:// and the www canonical version.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: