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:
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
blog comments powered by Disqus |
|
|
|
|
|
|
|