Creating a canonical with PHP

夙愿已清 提交于 2019-12-23 04:03:32

问题


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?


回答1:


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();



回答2:


$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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4837830/creating-a-canonical-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!