how to redirect domain according to country IP address how to redirect domain according to country IP address php php

how to redirect domain according to country IP address


Download the geoPlugin class from:

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

(free lookup limit of 120 requests per minute and block for 1h if crossed the limit. the block will automatically remove 1 hour after the last time your server stopped sending more than 120 requests a minute)

Put a index.php file in your root folder:

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


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

Then, tweak your .htaccess file accordingly :

GeoIPEnable OnGeoIPDBFile /path/to/GeoIP.dat# Start Redirecting countries# CanadaRewriteEngine onRewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$RewriteRule ^(.*)$ http://ca.abcd.com$1 [L]# IndiaRewriteEngine onRewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$RewriteRule ^(.*)$ http://in.abcd.com$1 [L]# etc etc etc...

And here's the official documentation.


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