Php 如何根据国家/地区IP地址重定向域
我用一些子域创建了一个站点;根据国家的IP地址,用户应该自动重定向到相应的子域 示例: 主站点是Php 如何根据国家/地区IP地址重定向域,php,.htaccess,redirect,geolocation,geotargetting,Php,.htaccess,Redirect,Geolocation,Geotargetting,我用一些子域创建了一个站点;根据国家的IP地址,用户应该自动重定向到相应的子域 示例: 主站点是abcd.com 假设来自印度的某个人键入了这个url abcd.com 然后页面重定向到ind.abcd.com 检查服务器上是否安装了模块(GeoIP Extension) 然后,相应地调整.htaccess文件: GeoIPEnable On GeoIPDBFile /path/to/GeoIP.dat # Start Redirecting countries # Canada Rewr
abcd.com
- 假设来自印度的某个人键入了这个url abcd.com
- 然后页面重定向到
ind.abcd.com
.htaccess
文件:
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat
# Start Redirecting countries
# Canada
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://ca.abcd.com$1 [L]
# India
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$
RewriteRule ^(.*)$ http://in.abcd.com$1 [L]
# etc etc etc...
以下是。从以下站点下载geoPlugin类: (免费查找限制为每分钟120个请求,如果超过此限制,则阻止1h。上次服务器停止发送每分钟超过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/');
}
?>
以下是国家代码列表:
您可以在不使用
require_once('geoplugin.class.php')的情况下完成此操作代码>像这样:
<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='US')
header( 'Location: http://example1.com' ) ;
else
header( 'Location: http://example2.com' ) ;
?>
如果您使用的是WordPress网站,那么它很容易使用-(地理重定向插件)。它很有魅力。易于使用,易于实施 GeoIP扩展允许您查找IP地址的位置。如何通过cpanel安装它?感谢简单有效的解决方案这就像一个魅力。谢谢。简单有效的解决方案。它似乎是一个高级插件。我发现这个是免费的:需要你从maxmind.com下载/上传一个IPs数据库。如果我错了,请纠正我,但使用这个插件,网站变得如此缓慢,因为这个API调用,我们已经放弃使用这个。