how to redirect domain according to country IP address

南楼画角 提交于 2019-11-26 04:36:19

问题


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.

Example :

Main site is abcd.com

  • Suppose some one from India typed this url abcd.com,
  • then the page redirects to ind.abcd.com

回答1:


Download the geoPlugin class from:

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

Put a index.php file in your root folder:

<?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




回答2:


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

Then, tweak your .htaccess file accordingly :

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:


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

<?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:


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.



来源:https://stackoverflow.com/questions/9838344/how-to-redirect-domain-according-to-country-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!