简体   繁体   English

用PHP创建规范

[英]Creating a canonical with PHP

I need PHP code to generate a dynamic canonical URL within the <link /> tag as follows: 我需要PHP代码在<link />标记内生成动态的规范URL,如下所示:

<link rel="canonical" href="php goes here" />

My site uses PHP to generate variables as follows: 我的网站使用PHP生成变量,如下所示:

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 我希望能够返回删除&pointlessvar=narrow的规范URL

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) 我想这样做是出于SEO的目的,因为我的网站包含许多不同顺序的变量,这些变量为基本相同的内容提供了不同的URL(以防止在SERPS中重复并集中链接汁)

Can anybody suggest some PHP code that I can place in the <link /> tag? 有人可以建议一些可以放在<link />标记中的PHP代码吗?

$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. php有更多可用的排序功能。
They even allow you to write your own custom sort function . 它们甚至允许您编写自己的自定义排序功能

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. 要创建一个规范的url,您实际上应该确保,只有您需要的参数,并且也将它们放在固定的顺序中。 This code does that. 这段代码做到了。 It filters the list of _GET paramters and build a new url with only the desired ones. 它过滤_GET参数列表,并仅使用所需的URL来构建新的URL。 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. 我使用array_filter,因为我不确定如果在数组上的foreach中取消设置数组元素会发生什么。

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

You can mix parse_url(); 您可以混合parse_url(); function and http_build_query() to rebuild your url. 函数和http_build_query()来重建您的网址。

$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. 然后遍历$ params以设置无用的变量,然后使用http_build_query进行重建。

You can use the $_SERVER superglobal and the $_GET superglobal to get the various parts of the url. 您可以使用$ _SERVER超全局变量和$ _GET超全局变量来获取URL的各个部分。 You can rearrange and filter them anyway you like. 您可以随时重新排列和过滤它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM