PHP
  Home arrow PHP arrow Page 3 - Managing Secure Protocol in Apache-Based Websites using PHP
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

Managing Secure Protocol in Apache-Based Websites using PHP
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-05-28


    Table of Contents:
  • Managing Secure Protocol in Apache-Based Websites using PHP
  • First Potential Problem: Duplicate Content!
  • Second Duplicate Content Solution
  • Second Potential Problem: 301 Redirection from non-secure to secure protocol

  • 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


    Managing Secure Protocol in Apache-Based Websites using PHP - Second Duplicate Content Solution
    ( Page 3 of 4 )

    SECOND SOLUTION: In all https (secure pages), place a link rel canonical tag in the <head> section of the main website templates pointing to the equivalent http version of the URLs.

    If the secure protocol already earns some Google page rank juice, using the meta no index tag is not the best solution. Instead, use the link rel canonical tag:

    <link rel="canonical" href="http://www.thisisyourwebsite.biz/" />

    To use this tag, place it somewhere on the <head> section of your website template. This is how it works: when the Google bot visits an https version of the URL, the server returns the https version but on the <head> section of the source code, you can see this tag: <link rel="canonical" href="http://www.thisisthehttpversion.biz/" /> Google will crawl the http version but not the https version.

    It acts like a 301 redirect, but the URL on the address bar is not even redirected at all. In this situation, Google will award any page rank or any URL properties to the canonical http version. Therefore, even if the https version is indexable, Google will only display the http version in its search results. And if  previously the https version has earned a Google page rank, it will now be transferred to the http version, which is the canonical version.

    A sample PHP script that will execute this job is:


    <?php

    $URL=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')

    {

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

    }

    ?>


    If your website is using osCommerce or other popular software packages, there is a more recommended PHP script to cover canonical issues -- not only the secure vs insecure issues, but also the non-www and www issues.

    In my article on using the link rel="canonical" tag to solve for canonical issues in Apache/PHP powered websites, I recommended this PHP script:

    <?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.'" />';

    ?>


    This script is only applicable if the canonical version is HTTP (not HTTPS), and uses the www version of the site. A sample canonical URL could be:

    http://www.thisisasampleurl.com



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