我有以下代码:
<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>
如果它是丹麦网站客户端,这会将用户重定向到该站点的丹麦 (da) 版本,这是主站点。这是极好的。
但是,我想,如果用户不是丹麦语,而是波兰语、德语等,它会将他们重定向到位于子域的网站的英文版本
http://en.mysite.com/
如何在现有代码中实现它?提前致谢!——弗雷德里克·安徒生
编辑
像这样的解决方案;
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
不起作用,因为它在重定向时会产生循环错误 - 至少在 Google Chrome 中是这样。
编辑 2
session_start();
if (isset( $_SESSION['redirect']))
{
// do nothing / continue with rest of page
}
else
{
$_SESSION['redirect'] = true;
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
exit();
}