Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Simple rel="canonical" value for all pages (by Corey Northcutt - http://www.northcutt.com)
- *
- * OPTIONAL VARIABLES:
- * $lcase: set to 1 if you would like to assume that URL's should always be lowercase.
- * $trailslash: 1 assumes always a trailing slash on directories. 2 assumes never a slash.
- *
- * POTENTIAL LIMITATIONS:
- * Assumes "www" is the desired version of the site.
- * Assumes that you are not using a period in your directory names (trailing slashes feature)
- * Assumes GET variables do not determine page contents.
- */
- function getCanonical($lcase=0,$trailslash=0) {
- // Set variables
- $requestedurl = $_SERVER["REQUEST_URI"];
- // Force "www"
- if (preg_match("/www/i", $_SERVER["SERVER_NAME"])) $canonical="http://".$_SERVER["SERVER_NAME"].$requestedurl;
- else { $url='www.'.$_SERVER["SERVER_NAME"]; $canonical="http://".$url.$requestedurl; }
- // Hack off GET variables
- if(strstr($canonical,"?")) {
- $explosi0n = explode("?",$canonical);
- $canonical = $explosi0n[0];
- }
- // Force lowercase
- if($lcase == 1) $canonical = strtolower($canonical);
- // Force in a trailing slash on directories
- if($trailslash == 1) {
- $explosi0n = explode("/",$canonical);
- $reverseEx = array_reverse($explosi0n);
- // if URI is a directory name rather than file name
- if(strlen($reverseEx[0] > 0)) {
- if(!strstr($reverseEx[0],".")) {
- if(!$reverseEx[0]{0} == "/") $canonical = $canonical . "/";
- }
- }
- }
- // Force NEVER a trailing slash on directories
- if($trailslash == 2) {
- $explosi0n = explode("/",$canonical);
- $reverseEx = array_reverse($explosi0n);
- // if URI is a directory name rather than file name
- if(strlen($reverseEx[0] > 0)) {
- if(!strstr($reverseEx[0],".")) {
- if(!$reverseEx[0]{0} == "/") $canonical = substr($canonical,0,strlen($canonical)-1);
- }
- }
- }
- // return $canonical
- return $canonical;
- }
- echo '<link rel="canonical" href="'.getCanonical(1,2).'" />';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement