我用一些子域创建了一个站点;根据国家的 IP 地址,用户应该被自动重定向到相应的子域。
示例:
主要网站是 abcd.com
- 假设来自印度的某个人输入了这个 url abcd.com,
- 然后页面重定向到
ind.abcd.com
最佳答案
从以下位置下载 geoPlugin 类:
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
(免费查询限制为每分钟 120 个请求,如果超过限制将阻止 1 小时。阻止将在上次服务器停止每分钟发送超过 120 个请求后 1 小时自动移除)
将index.php 文件放在您的根文件夹中:
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "AL") {
header('Location: http://sq.wikipedia.org/');
}
else if ($var_country_code == "NL") {
header('Location: http://nl.wikipedia.org/');
}
else {
header('Location: http://en.wikipedia.org/');
}
?>
这是国家代码列表:
关于php - 如何根据国家IP地址重定向域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838344/