繁体   English   中英

如何根据国家IP地址重定向域

[英]how to redirect domain according to country IP address

我用一些子域创建了一个站点; 根据国家/地区的 IP 地址,用户应该被自动重定向到相应的子域。

例子 :

主要网站是abcd.com

  • 假设来自印度的某个人输入了这个网址 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/');
}
?>

以下是国家代码列表:

http://www.geoplugin.com/iso3166

检查您的服务器上是否安装了mod_geoip模块( GeoIP Extension )。

然后,相应地调整您的.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...

这是官方文档

你可以不用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 网站,那么它很容易使用 - (地理重定向插件)。 它像魅力一样工作。 易于使用,易于实施。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM