Mobile app version of vmapp.org
Login or Join
Becky754

: 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

@Becky754

Posted in: #301Redirect #Redirects

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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Becky754

1 Comments

Sorted by latest first Latest Oldest Best

 

@Hamaas447

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;
}
}

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme