【发布时间】:2015-10-26 23:09:22
【问题描述】:
我有 3 个针对不同地区的网站,它们都有自己的位置特定内容:
- 法国网站:http://dev.logichub.net/demo/fr/
- 德国网站:http://dev.logichub.net/demo/de/
- 默认网站:http://dev.logichub.net/demo/
我想根据访问者的 IP 地址将访问者重定向到相应的网站。例如,如果用户从德国登陆http://dev.logichub.net/demo/fr/,那么他/她必须被重定向到德国网站,即http://dev.logichub.net/demo/de/。我想在所有 3 个网站上实现相同的行为。
我在这3个网站的首页上放了如下代码:
// get visitor IP address and process
$geo_data = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( ! empty( $geo_data ) ) {
switch ( $geo_data['geoplugin_countryCode'] ) {
// redirect French visitors to FR website
case "FR":
echo '<script>window.location.replace( "http://dev.logichub.net/demo/fr/" );</script>';
break;
// redirect German visitors to DE website
case "DE":
echo '<script>window.location.replace( "http://dev.logichub.net/demo/de/" );</script>';
break;
// redirect all others to international website
default:
echo '<script>window.location.replace( "http://dev.logichub.net/demo/" );</script>';
}
}
除了重定向网站上的无限循环外,它的工作原理与我想要的完全一样。访问者被完美重定向,但新网站永远加载。如何停止无限循环,以便一旦访问者重定向到正确的网站,就不会再发生重定向?
【问题讨论】:
标签: infinite-loop url-redirection geoip