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?

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.

commented: Very helpful resource +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.