Help redirecting based on Country

zelvmoney

New member
Nov 4, 2008
64
1
0
Hi everyone!


What I need to know is if I can redirect a visitor based on his country.

I mean, I know I can do that with a php file, calling the GeoIp database, but to do it I have to put it on my domain root and mysite.com becames a redirection script to mysite.com/uk and mysite.com/us, etc.

Here´s the php example script:

Code:
<?php

require_once("geoip/geoip.inc");

$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

geoip_close($gi);

if($country_code == 'fr')
{
 header("HTTP/1.1 301 Moved Permanently");
 header('Location: http://mysite.com/fr/');
}
else
{
 header("HTTP/1.1 301 Moved Permanently");
 header('Location: http://mysite.com/en/');
}
?>
What I want is to know if it´s possible to redirect using .htaccess or any other method, or just using the php method I refered above.

Because what I want is something like this:

- visitors click on a link to mysite.com and if it is US citizen nothing happens and he enter on mysite.com, if it´s French or Brazilian, enter on mysite.com/international/

Besides that, if possible, I also want this:

- visitor click on a link to mysite.com/hosting/ and if it is a US citizen nothing happens and he enter on mysite.com/hosting/, if it´s French or Brazilian, enter on mysite.com/hosting/international/

Conclusion:

- for every page on my site, I want to have a “mirror” version (with a no index tag), just with different banners and different small portions of text for where I want to redirect all my international traffic.

If anyone could help me I would be very appreciated.
 


Perfectly doable with .htaccess, see here:

MaxMind - GeoIP Apache API

You're going to need to be able to install the Apache module, so if you're on shared hosting you're probably out of luck (or maybe they've already got it installed).

Alternatively, just include the PHP script at the top of every page. I believe the .htaccess way is faster, but unless you're serving up a LOT of impressions, the difference is probably negligible.