0

Hello to everyone,
I want to direct foreign visitors of my site in one directory, this based on their IP.
My site structure is:

index.php(file)
|->foreign(directory)

I think that this can be done with .htaccess, but I don't know how

Can someone gives me an example how to do this?

2
Contributors
1
Reply
11
Views
4 Years
Discussion Span
Last Post by cereal
2

You can use GeoIP library, for example:

$countrycode = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
$loc = '/default/contents';
switch($countrycode)
{
    case 'FR':
        $loc = '/fr/contents';
    break;
    case 'IT':
        $loc = '/it/contents';
    break;
    case 'US':
        $loc = '/us/contents';
    break;
}

header('Location: '.$loc);

at that point you can also place a cookie to switch automatically each time the user comes back, but in order to use it you have to install the library:

This can be done with PECL or by command line, in debian/ubuntu type:

sudo apt-get install php5-geoip

Note, some of the functions as geoip_region_* will not work if you don't load the correct database, some of them are only available by payment, by default you get the Country database both IPV4 and IPV6, there is also the City db available for free:

In case you want to use it: download it, use gunzip to decompress the file gunzip GeoLiteCity.dat.gz and place it inside /usr/share/GeoIP/

EDIT
As side note, this can work also with other free GeoIP databases, but I didn't tested.

Hope is useful, bye.

Edited by cereal

Votes + Comments
Very helpful resource
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.