Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to redirect domain according to country IP address

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
like image 947
user1288065 Avatar asked Mar 23 '12 11:03

user1288065


People also ask

Can I redirect a domain to a specific URL?

What is URL redirect? URL redirect (URL forwarding) allows you to forward your domain visitors to any URL of your choice (to a new domain or a different website). You can set 301 (Permanent), 302 (Unmasked), and Masked (URL Frame) redirects for the domain names pointed to BasicDNS, PremiumDNS or FreeDNS.

How do I redirect a domain to traffic?

Under the Domain category, choose the Redirects menu. You'll see the Create a Redirect section. Here, you'll need to fill in which URL you want to Redirect and where you want it to Redirect To. Make sure your information is correct and choose the right connection protocol – HTTP or HTTPS.


2 Answers

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:

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

like image 99
Porta Shqipe Avatar answered Sep 21 '22 00:09

Porta Shqipe


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.

like image 36
Dr.Kameleon Avatar answered Sep 21 '22 00:09

Dr.Kameleon