Want more features on Pastebin? Sign Up, it's FREE!

getCanonical() PHP

By: qwidjib0 on Dec 8th, 2011  |  syntax: PHP  |  size: 1.92 KB  |  views: 855  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. * Simple rel="canonical" value for all pages (by Corey Northcutt - http://www.northcutt.com)
  3. *
  4. * OPTIONAL VARIABLES:
  5. * $lcase:  set to 1 if you would like to assume that URL's should always be lowercase.
  6. * $trailslash:  1 assumes always a trailing slash on directories.  2 assumes never a slash.
  7. *
  8. * POTENTIAL LIMITATIONS:
  9. * Assumes "www" is the desired version of the site.
  10. * Assumes that you are not using a period in your directory names (trailing slashes feature)
  11. * Assumes GET variables do not determine page contents.
  12. */
  13. function getCanonical($lcase=0,$trailslash=0) {
  14.         // Set variables
  15.         $requestedurl = $_SERVER["REQUEST_URI"];
  16.        
  17.         // Force "www"
  18.         if (preg_match("/www/i", $_SERVER["SERVER_NAME"])) $canonical="http://".$_SERVER["SERVER_NAME"].$requestedurl;
  19.         else { $url='www.'.$_SERVER["SERVER_NAME"]; $canonical="http://".$url.$requestedurl; }
  20.        
  21.         // Hack off GET variables
  22.         if(strstr($canonical,"?")) {
  23.                 $explosi0n = explode("?",$canonical);
  24.                 $canonical = $explosi0n[0];
  25.         }
  26.        
  27.         // Force lowercase
  28.         if($lcase == 1) $canonical = strtolower($canonical);
  29.        
  30.         // Force in a trailing slash on directories
  31.         if($trailslash == 1) {
  32.                 $explosi0n = explode("/",$canonical);
  33.                 $reverseEx = array_reverse($explosi0n);
  34.                 // if URI is a directory name rather than file name
  35.                 if(strlen($reverseEx[0] > 0)) {
  36.                         if(!strstr($reverseEx[0],".")) {
  37.                                 if(!$reverseEx[0]{0} == "/") $canonical = $canonical . "/";
  38.                         }
  39.                 }
  40.         }
  41.        
  42.         // Force NEVER a trailing slash on directories
  43.         if($trailslash == 2) {
  44.                 $explosi0n = explode("/",$canonical);
  45.                 $reverseEx = array_reverse($explosi0n);
  46.                 // if URI is a directory name rather than file name
  47.                 if(strlen($reverseEx[0] > 0)) {
  48.                         if(!strstr($reverseEx[0],".")) {
  49.                                 if(!$reverseEx[0]{0} == "/") $canonical = substr($canonical,0,strlen($canonical)-1);
  50.                         }
  51.                 }
  52.         }
  53.        
  54.         // return $canonical
  55.         return $canonical;
  56. }
  57. echo '<link rel="canonical" href="'.getCanonical(1,2).'" />';
  58.  
clone this paste RAW Paste Data