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

时间:2021-11-14 02:57:30

I made a site it with some subdomains; according to the country's IP address the user is supposed to be automatically redirected to corresponding subdomain.

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

Example :

示例:

Main site is abcd.com

主要网站是abcd.com

  • Suppose some one from India typed this url abcd.com,
  • 假设有一个来自印度的人输入了这个网址abcd.com,
  • then the page redirects to ind.abcd.com
  • 然后页面重定向到ind.abcd.com

4 个解决方案

#1


21  

Download the geoPlugin class from:

从以下位置下载geoPlugin类:

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

Put a index.php file in your root folder:

将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/');
}
?>

Here is a list of country codes:

以下是国家/地区代码列表:

http://www.geoplugin.com/iso3166

http://www.geoplugin.com/iso3166

#2


20  

Check that you have the mod_geoip module (GeoIP Extension) installed on your server.

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

Then, tweak your .htaccess file accordingly :

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

And here's the official documentation.

这是官方文档。

#3


12  

You could do this without require_once('geoplugin.class.php'); like so:

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


0  

If you are using a WordPress website, so its easy to use - (Geo Redirect plugin). It's working like charm. Easy to use, easy to implement.

如果您使用的是WordPress网站,那么它易于使用 - (Geo Redirect插件)。它的工作就像魅力一样。易于使用,易于实施。

#1


21  

Download the geoPlugin class from:

从以下位置下载geoPlugin类:

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

Put a index.php file in your root folder:

将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/');
}
?>

Here is a list of country codes:

以下是国家/地区代码列表:

http://www.geoplugin.com/iso3166

http://www.geoplugin.com/iso3166

#2


20  

Check that you have the mod_geoip module (GeoIP Extension) installed on your server.

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

Then, tweak your .htaccess file accordingly :

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

And here's the official documentation.

这是官方文档。

#3


12  

You could do this without require_once('geoplugin.class.php'); like so:

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


0  

If you are using a WordPress website, so its easy to use - (Geo Redirect plugin). It's working like charm. Easy to use, easy to implement.

如果您使用的是WordPress网站,那么它易于使用 - (Geo Redirect插件)。它的工作就像魅力一样。易于使用,易于实施。