PHP 重定向 Accept_Language
PHP Redirecting by Accept_Language
好的,我是 PHP 的新手,所以如果我在这里犯了一些非常基本的错误,请多多包涵:
我正在尝试获取 magento 1。9.x 商店按语言重定向到子商店。我做了这个:
function checkStoreLanguage()
{
$result = '';
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langString = strtolower(substr( $_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2 ));
if($langString == 'da'){
$result = '/dk';
} elseif ($langString == 'en'){
$result = '/uk';
} else {
$result = '/eu';
}
}
return $result;
}
if ($_SERVER['REQUEST_URI'] === '/') {
header('Location: '.checkStoreLanguage());
exit;
}
现在它似乎可以在隐身模式下工作,但不能在正常模式下工作,所以它可能是缓存问题,但缓存真的会影响服务器重定向吗?我该如何避免这种情况?
您可以将 HTTP 响应代码设置为 303
以告知浏览器它不应被缓存。
The 303
response MUST NOT be cached, but the response to the second
(redirected) request might be cacheable.
在PHP内:
header('Location: ' . checkStoreLanguage(), true, 303);
顺便说一下:当请求中没有 Accept-Language
-header 时,您的代码当前重定向到 'empty string'。您可能希望通过将 $result
变量初始化为您的(默认值?)'eu' 来更改它。因此 $result = 'eu'
而不是 $result = ''
.
好的,我是 PHP 的新手,所以如果我在这里犯了一些非常基本的错误,请多多包涵:
我正在尝试获取 magento 1。9.x 商店按语言重定向到子商店。我做了这个:
function checkStoreLanguage()
{
$result = '';
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langString = strtolower(substr( $_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2 ));
if($langString == 'da'){
$result = '/dk';
} elseif ($langString == 'en'){
$result = '/uk';
} else {
$result = '/eu';
}
}
return $result;
}
if ($_SERVER['REQUEST_URI'] === '/') {
header('Location: '.checkStoreLanguage());
exit;
}
现在它似乎可以在隐身模式下工作,但不能在正常模式下工作,所以它可能是缓存问题,但缓存真的会影响服务器重定向吗?我该如何避免这种情况?
您可以将 HTTP 响应代码设置为 303
以告知浏览器它不应被缓存。
The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.
在PHP内:
header('Location: ' . checkStoreLanguage(), true, 303);
顺便说一下:当请求中没有 Accept-Language
-header 时,您的代码当前重定向到 'empty string'。您可能希望通过将 $result
变量初始化为您的(默认值?)'eu' 来更改它。因此 $result = 'eu'
而不是 $result = ''
.