这是检测浏览器语言并将301重定向到主页的最友好的方式 [英] Which is the most seo-friendly way to detect browser language and redirect 301 to home page

查看:204
本文介绍了这是检测浏览器语言并将301重定向到主页的最友好的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多语言页面,我想检测客户端浏览器的语言,然后制作301主页或其他内容。但我不确定哪种方式对seo更好。我不知道网络蜘蛛喜欢哪一个?或者其他方式?

I have a multi-languagepage, I want to detect client browser's language then make a 301 home page or other thing. but I am not sure which way is better for seo. I do not know web spider like which one? Or other way?

<?php 
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE']; 
if (preg_match('/^[zZ][hH]/', $LG)) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/cn/");
exit();} //jump to chinese version
else { 
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/en/");
exit();} //jump to english version
?>

OR

<?php 
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE']; 
if (preg_match('/^[zZ][hH]/', $LG)) {
include ("http://mydomain.com/cn/");
} //include chinese version
else { 
header("HTTP/1.1 301 Moved Permanently");
include ("http://mydomain.com/en/");
} //include english version
?>

或其他方式?谢谢。

推荐答案

正如您在问题中已经假设的那样,您需要解析 Accept-Language HTTP / 1.1 标头,在PHP中的 $ _ SERVER ['HTTP_ACCEPT_LANGUAGE'] 中可用。首先需要将其解析为一个可以在PHP中更好地处理的结构,如数组:

As you already assume in your question, you need to parse the Accept-LanguageHTTP/1.1 header, which is available in PHP in $_SERVER['HTTP_ACCEPT_LANGUAGE']. This first needs to be parsed into a structure you can better deal within PHP, like an array:

/**
 * Convert Accept Language to sorted PHP array
 * 
 * Related HTTP Specs:
 *   <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4>
 *   <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9>
 *
 * @param string $accept header value
 * @return array ([language-range] => qvalue, ...)
 */
function http_accept_language_array($accept = NULL)
{
    if (!$accept && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $accept = (string) $accept;

    $pattern = '/([a-z]{1,8}(-[a-z]{1,8})?)(;q=([01](?:\.[0-9]{0,3})?))?(?=$|,[ ]*)/i';
    preg_match_all($pattern, $accept, $matches);

    $array = array();
    if (count($matches[1]))
    {
        list(, $ranges,,, $qvals) = $matches;
        # normalize ranges
        foreach ($ranges as &$range)
            $range = strtolower($range);
        unset ($range);
        # set default qvalue 1
        foreach ($qvals as &$qval)
            if ('' === $qval) $qval = '1';
        unset ($qval);        
        $array = array_combine($ranges, $qvals);
        arsort($array, SORT_NUMERIC);
    }
    return $array;
}

da,en-gb; q = 0.8,en; q = 0.7 将返回:

array(3) {
  ["da"]    => string(1) "1"
  ["en-gb"] => string(3) "0.8"
  ["en"]    => string(3) "0.7"
}

然后你需要将这个已排序的数组解析为找到您的第一场比赛,使用 en 默认值设置您的偏好:

You then need to parse this sorted array to find your first match, setting your preference with the en default value:

$lang = 'en';
foreach (http_accept_language_array() as $range => $qvalue)
{
    if (preg_match('/^zh[$-]/', $range))
    {
        $lang = 'cn';
        break;
    }
}

最后你可以根据<$ c进行重定向$ c> $ lang (或包含或其他):

Finally you can do the redirect based on $lang (or the include or whatever):

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/$lang/");

如果您正在寻找一个现成的库来处理这个问题,一个现有的解决方案是 Symfony HttpFoundation\Request 或在PEAR中有 HTTP :: negotiateLanguage

If you're looking for a ready-made library to deal with this, one existing solution is the Symfony's HttpFoundation\Request or in PEAR there is HTTP::negotiateLanguage.

PHP intl扩展还有另一个相关的低级函数,但是它不提供数组而是一个值: locale_accept_from_http

The PHP intl extension has another low-level function that is related, however it's doesn't offer an array but a single value: locale_accept_from_http

更多HTTP相关信息的另一个常规资源是 PHP中HTTP请求的高级处理

Another general resource for more HTTP related information is Advanced handling of HTTP requests in PHP.

这篇关于这是检测浏览器语言并将301重定向到主页的最友好的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆