【发布时间】:2018-08-29 22:18:11
【问题描述】:
我有一个用于 cloudflare 的 georedirect 代码,如下所示
<?php
$URI = $_SERVER['REQUEST_URI'];
$activepage = $_SERVER['SERVER_NAME'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if ($activepage=="www.example.co.uk" & $country_code=="CA") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.co.uk" & $country_code=="US") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="GB") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="UK") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}
?>
有效地将英国/英国用户从美国网站重定向回英国网站,并将英国网站上的美国和加拿大用户重定向到美元网站 (.us)
我想添加一个最后的 else 语句,以便美国网站上所有非美国或加拿大的流量都被重定向到英国网站(对于世界其他地方也是如此)。
我最好怎么做?
另外,这似乎需要很长时间才能运行(在一台好的服务器上)我如何才能简化它,让它只运行到它需要的地方?例如。遇到正确的elseif后,其他elseif不在后台运行吗?我以为 exit();会做的伎俩,但显然不是。
谢谢
【问题讨论】:
-
使用
&&而不是按位&。 -
谢谢@PHPglue 的意义何在?
-
@PHPglue 我认为 elseif ($activepage=="www.example.us") 的最终声明将涵盖所有其他国家,但显然这不仅仅是挂起和冻结?
-
你应该可以在你最后的
elseif后面加上die;或exit;(括号不是真的需要)。无论如何,当 Apache 处理完页面后,页面应该die;,所以我不能说为什么这需要任何时间才能运行。
标签: php redirect server header geolocation