: Is it possible to do 301 redirects by country? We have a .com website but operate in the UK and the US, we want the .com to 301 to the .co.uk website but for people in the UK only and
We have a .com website but operate in the UK and the US, we want the .com to 301 to the .co.uk website but for people in the UK only and the .com to remain as it is.
Is this possible with 301 - if not whats the best way around this?
More posts by @Becky754
1 Comments
Sorted by latest first Latest Oldest Best
Using .htaccess
You do this with a simple .htaccess rule by detecting the browsers Accept-Language (read here).
RewriteEngine on
RewriteCond %{HTTP:Accept-language} ^en-GB [NC]
RewriteRule ^$ example.co.uk [L,R=301]
Using PHP
You can do this with a programming language to perform a lookup (I generally use a 3rd party open API) then send the 301 header if it falls within your criteria.
For example, using PHP we can do the following;
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.geoplugin.net/json.gp?ip=". $_SERVER['REMOTE_ADDR'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
//There was an error with the curl request
//Don't do anything...
} else {
$res = json_decode($response, true);
if( strtoupper($res->geoplugin_countryCode) == "GB" ) {
//Issue the 301
header("Location: example.co.uk , TRUE, 301);
die;
}
}
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.