PHP
  Home arrow PHP arrow Page 4 - Using the Link Rel Canonical Tag
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using the Link Rel Canonical Tag
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-05-07


    Table of Contents:
  • Using the Link Rel Canonical Tag
  • Theory of Implementation
  • Implementation Steps
  • Implementing a Canonical Version, First Scenario
  • Implementing Canonical Version for a Secure Website

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using the Link Rel Canonical Tag - Implementing a Canonical Version, First Scenario
    ( Page 4 of 5 )

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek