Home arrow PHP arrow Page 3 - Managing Secure Protocol in Apache-Based Websites using PHP

Second Duplicate Content Solution - PHP

When trying to maintain a secure protocol on an Apache-based website, you can expect to deal with certain issues, especially if you're also trying to rank well in the search engines. This article provides you with some solutions for two of the more difficult problems: duplicate content and 301 redirects.

  1. Managing Secure Protocol in Apache-Based Websites using PHP
  2. First Potential Problem: Duplicate Content!
  3. Second Duplicate Content Solution
  4. Second Potential Problem: 301 Redirection from non-secure to secure protocol
By: Codex-M
Rating: starstarstarstarstar / 5
May 28, 2009

print this article



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:



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:


//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


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




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




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




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;



//no session ID




//trim the URLs any session ID


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


//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





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




//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:


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: