how to redirect domain according to country IP address

user1288065 picture user1288065 · Mar 23, 2012 · Viewed 80.1k times · Source

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

Answer

Porta Shqipe picture Porta Shqipe · Jul 30, 2014

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