【问题标题】:how to redirect domain according to country IP address如何根据国家IP地址重定向域
【发布时间】:2012-04-07 23:06:02
【问题描述】:

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

示例:

主站点是abcd.com

  • 假设某个来自 印度 的人输入了这个网址 abcd.com,
  • 然后页面重定向到ind.abcd.com

【问题讨论】:

标签: php .htaccess redirect geolocation geotargetting


【解决方案1】:

从以下位置下载 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

【讨论】:

  • 如果我错了,请纠正我,但是使用这个插件,由于这个 API 调用,网站变得很慢,我们放弃了使用这个。
【解决方案2】:

如果您使用的是 WordPress 网站,那么它很容易使用 - (地理重定向插件)。它就像魅力一样工作。易于使用,易于实施。

【讨论】:

【解决方案3】:

你可以在没有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' ) ;

?>

【讨论】:

  • 这就像一个魅力。非常感谢。简单有效的解决方案。
【解决方案4】:

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

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

这是official documentation

【讨论】:

  • 如何通过cpanel安装?
  • 感谢简单有效的解决方案
猜你喜欢
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2010-10-09
  • 2013-10-25
相关资源
最近更新 更多