Reputation: 6744
I am trying to create auto redirect for my website based on a country of a user. The website will be available only in 1 language but pricing will be based on a country currency. It's a simple website with main page, terms and conditions, privacy policy and that's all. My hosting provider is OVH and I suppose they have Apache servers.
I was trying to do it with php but it didn't work as intended. Servers are located in France and I was redirected to http://www.test.com
PHP:
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if ($country_code=="PL") {$link = 'http://www.example.com';}
elseif ($country_code=="CA") {$link = 'http://www.example.com/ca';}
elseif ($country_code=="US") {$link = 'http://www.example.com/us';}
else {$link = 'http://www.test.com';}
header("location:$link");
exit;
I tried editing htaccess file but nothing happened:
RewriteEngine On
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^PL$
RewriteRule ^(.*)$ http://www.example.com [L]
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^CA$
RewriteRule ^(.*)$ http://www.example.com/ca [L]
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^US$
RewriteRule ^(.*)$ http://www.example.com/us [L]
Any ideas how can I do it in the easiest possible way?
Upvotes: 0
Views: 1427
Reputation: 336
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...
Documentation: URL
Upvotes: 1