[英]This webpage has a redirect loop "ERR_TOO_MANY_REDIRECTS" in chrome and all browser in php code
[英]ERR_TOO_MANY_REDIRECTS (PHP) (Detect browser lang and redirect)
我有这个 PHP 代码,它检测浏览器语言并将用户重定向到我网站的语言版本。 如果该版本不存在,则将用户重定向到英文版本。 我的主要语言是西班牙语,其路径是: https://example.com/
问题是,如果用户说西班牙语,代码会将用户重定向到相同的路径: https://example.com/
并导致错误ERR_TOO_MANY_REDIRECTS 。
我如何告诉脚本如果用户说西班牙语,它不会重定向并会留在那里? (我可以说代码在重定向到英语和葡萄牙语时效果很好)
PS:我尝试使用return false
但页面仍然空白。
提前致谢。
<?php
$accept_languaje = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'es';
$main_languaje = substr($accept_languaje, 0, 2);
switch ($main_languaje):
case 'es':
exit(header("Location: https://example.com/"));
case 'en':
exit(header("Location: https://example.com/en.php"));
case 'pt':
exit(header("Location: https://example.com/pt.php"));
endswitch;
if (false !== strpos($accept_languaje, 'es')) {
exit(header("Location: https://example.com/"));
}
if (false !== strpos($accept_languaje, 'en')) {
exit(header("Location: https://example.com/en.php"));
}
if (false !== strpos($accept_languaje, 'pt')) {
exit(header("Location: https://example.com/pt.php"));
}
exit(header("Location: https://example.com/en.php"));
?>
删除以下内容并尝试
case 'es':
exit(header("Location: example.com/"));
if (false !== strpos($accept_languaje, 'es')) { exit(header("Location: example.com/")); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.