PHP
  Home arrow PHP arrow Page 5 - 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 Canonical Version for a Secure Website
    ( Page 5 of 5 )

    This section explains how to implement the second scenario, where the canonical version of the site includes https:// and www. It is relatively easy to revise the PHP script from the previous section if your canonical version is https and www. The only thing that needs to be revised is this:

    $protocol='http://';

    Change that to:

    $protocol='https://';

    It should then return the secure protocol version of the domain as the canonical version.

    By the way, when the server executes the PHP script above (say we are using the https// version), it should return this HTML code in the browser:

    <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html dir="LTR" lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title>OsCommerce Test Store</title>

    <base href="http://localhost/osc/">

    <link rel="stylesheet" type="text/css" href="stylesheet.css">

    <link rel="canonical" href="https://www.localhost/osc/index.php?cPath=3" />

    </head>

    Implementation of the Third Scenario

    If you're trying to implement http:// and the non-www version of your website as the canonical version, you should know that this is a little bit trickier. Most domains are configured to have a www in the beginning of the server name, and if you want to strip off the www as part of the URL, then we need to approach it in a way somewhat different from the string URL manipulation we saw in the first and second scenarios. 

    <?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 NON-WWW VERSION.

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

    //Example: if your canonical version is http://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

    //strip the www as part of the URL then display the canonical version without it and the session ID.

    $wwwversion = $_SERVER["SERVER_NAME"];

    $charactercount=strlen($wwwversion);

    $wwwstripoff=substr($wwwversion,4,$charactercount);

    $canonical=$protocol.$wwwstripoff.$cleanrequest;

    }

    else

    {

    //already using the non-www URL, display the canonical version:

    $URL=$_SERVER["SERVER_NAME"];

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

    }

    //Final step defining the final link rel canonical element

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

    ?>

    For the fourth scenario (https and non-www version), using the PHP script for the third scenario discussed above, revise:

    $protocol='http://';

    Change that to:

    $protocol='https://';

    This will return the https protocol using non-www as the canonical version.

    Summary

    This PHP script can be used to solve URL canonical problems with the following scope of applications:

    • This cannot used in a sub-domain of a main domain.
    • This can be applied in the following kinds of PHP-powered sites only: WordPress, OsCommerce, Zen Cart, CRE Loaded.
    • This can be used in Apache-powered PHP websites only.

    If you would like to download the PHP scripts file, visit this URL: http://www.php-developer.org/2009/03/link-relcanonical-tag-php-script.html



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek