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
?>
OR other way? Thanks.
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:Which for
da, en-gb;q=0.8, en;q=0.7will return:You then need to parse this sorted array to find your first match, setting your preference with the
endefault value:Finally you can do the redirect based on
$lang(or the include or whatever):If you’re looking for a ready-made library to deal with this, one existing solution is the Symfony’s
HttpFoundation\Requestor in PEAR there isHTTP::negotiateLanguage.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_httpAnother general resource for more HTTP related information is Advanced handling of HTTP requests in PHP.