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

Implementing Canonical Version for a Secure Website - 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.

  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



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:


Change that to:


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


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


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. 


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


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

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

$wwwversion = $_SERVER["SERVER_NAME"];







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




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


Change that to:


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


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

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: