Creating a canonical with PHP
I need PHP code to generate a dynamic canonical URL within the <link />
tag as follows:
<link rel="canonical" href="php goes here" />
My site uses PHP to generate variables as follows:
http://www.mysite.com/script.php?var1=blue&var2=large&pointlessvar=narrow
I开发者_开发知识库 want to be able to return a canonical URL that removes the &pointlessvar=narrow
And re-arranges the variables in the manner as I see fit, like this:
<link rel="canonical" href="http://www.mysite.com/script.php?var2=large&var1=blue" />
I want to do this for SEO purposes as my site contains many variables in different orders that give different URL'S for essentially the same content (to prevent duplication in the SERPS and to concentrate the link juice)
Can anybody suggest some PHP code that I can place in the <link />
tag?
To make a canonical url, you should actually make sure, you got only the parameters you need and put them in a fixed order too. This code does that. It filters the list of _GET paramters and build a new url with only the desired ones. I put it some comments, so you can easily adjust this code to fit your needs.
I use array_filter, because I'm not sure what happens if you unset array elements within a foreach on the array.
function params()
{
return array('b', 'c', 'a', 'z');
}
function checkParam($a)
{
// Checks if key $a is in array of valid parameters
return in_array($a, params());
}
function compare($a, $b)
{
return array_search($a, params()) - array_search($b, params());
}
function getCanonicalUrl()
{
$querystring = '';
// Copy and flip the array to allow filtering by key.
$params = array_flip($_GET);
// Filter out any params that are not wanted.
$params = array_filter($params, 'checkParam');
// If none remain, we're done.
if (count($params) !== 0)
{
// Sort the rest in given order
uasort($params, 'compare');
// Create a query string. Mind, name and value are still flipped.
$querystring = '?'.http_build_query(array_flip($params));
}
return
'http://'.
// $_SERVER['HTTP_HOST'] .
$_SERVER['SCRIPT_NAME'] .
$querystring;
}
print getCanonicalUrl();
$path = "http://www.mysite.com/script.php?var1=blue&var2=large&pointlessvar=narrow";
$url = parse_url($path, PHP_URL_QUERY); // Fetch the query component of a url
// Put the query into an array with the var name as the key
parse_str($url, $query=array());
foreach ($query as $name=>$val) {
// Check for pointless vars and unset() them here
}
krsort ($query); // Sort by array keys in reverse order.
$pathex = explode('?', $path, 2);
$npath = $pathex[0] . '?' . http_build_query($query);
There are more sort function available by php.
They even allow you to write your own custom sort function.
You can mix parse_url();
function and http_build_query()
to rebuild your url.
$url = 'http://www.mysite.com/script.php?var1=blue&var2=large&pointlessvar=narrow';
$url = parse_url($url);
$params = array();
$tmpParams = explode('&',$url['query']);
foreach ($tmpParams as $param) {
$tmp = explode('=', $param);
$params[$tmp[0]] = (!empty($tmp[1])) ? $tmp[1] : null;
}
Then loop through $params to unset useless variables and then rebuild with http_build_query.
You can use the $_SERVER superglobal and the $_GET superglobal to get the various parts of the url. You can rearrange and filter them anyway you like.
精彩评论