FrancisMV123
FrancisMV123

Reputation: 3449

Redirect users according to country -HTACCESS?

. Consider my domain name is www.example.com . It is in english

And i have have one language version for dutch www.example.com/nl

I want to redirect url to www.example.com/nl only for people requesting from Netherland.

My website is in shared hosting. How can i achieve this ?

Upvotes: 1

Views: 8156

Answers (3)

Douglish
Douglish

Reputation: 293

You have to use GeoIP service and determine country of your visitor and then do the proper redirect.

Download Geo API for PHP here http://geolite.maxmind.com/download/geoip/api/php/

There are samples how to use the code.

It would be something like

include("geoipcity.inc");
include("geoipregionvars.php");

$gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$_SERVER['REMOTE_ADDR']);

if ( $record->country_code == "NL" ) {
    header('Location: http://www.yoursite.com/nl');
}

Upvotes: 3

davidethell
davidethell

Reputation: 12018

You can redirect based on the browser's language setting coming from the user:

RewriteCond %{HTTP:Accept-language} nl [NC]
RewriteRule ^$ /nl [L,R=301]

Upvotes: 5

kalpaitch
kalpaitch

Reputation: 5271

Take a look at the apache module mod_geoIP, might do what you want, but otherwise you're going to have to rely on a script to get the geolocation information and redirect based on that.

As a side topic, IP lookup isn't 100% accurate in regards to getting geolocation information, so be careful. Often it is a better idea to let the user choose first time round, and then save a cookie to remember this choice.

Edit: And yes @Pekka has a good point, there is tons of information about it on Google.

Upvotes: 2

Related Questions