Redirect Entire Country (UK) from .com to .co.uk via htaccess

Status
Not open for further replies.

unrealindeed

Administrator
Staff member
Hi All,

I have launched a .com website priced in € for my client, and they have asked me to setup a .co.uk (specifically for the UK market). A copy of the site was made and prices etc were all changed to £ GBP. My client has now asked me to redirect all UK users from the .com to the .co.uk.

I have been looking into this, and the solution I have come up with is to visit blockacountry.com and get the entire list (Most up to date) of UK IP Addresses.
The website give me the code for the htaccess file to block them, but I want to redirect them from .com to .co.uk so they can only buy in £ GBP

An example of the code I download from blockacountry.com is


Code:
deny from 217.78.144.0/20
  deny from 217.79.96.0/20
  deny from 217.79.112.0/20
  deny from 217.79.160.0/20
  deny from 217.79.192.0/20
  deny from 217.79.240.0/20
  deny from 217.110.0.0/15
  deny from 217.112.32.0/20
Can anyone help me rewrite this to redirect thses IP’s instead?

I need to incorporate the above IP’s (there is many more, these are just a few) to the below code I htaccess

Code:
RewriteCond %{HTTP_HOST} ^www.my-domain.com$ [NC]
  RewriteRule ^(.*)$ http:// my-domain.co.uk/$1 [R=301,L]
Or is there a better solution to having all UK clients pointed to the .co.uk website?
Also, how would I go about testing this, uk proxy maybe?

Many thanks in advance.
 

mneylon

Administrator
Staff member
Use MaxMind it's a lot more accurate and a hell of a lot saner
 

unrealindeed

Administrator
Staff member
Really? That looks all to confusing for me! Ill have to read though it and see what i can do. Its a Joomla/Virtmart website (PHP) so I will have a look at this & see what i can make out of it, thank you Blacknight (again). Always to my rescue.
 

mneylon

Administrator
Staff member
Really? That looks all to confusing for me! Ill have to read though it and see what i can do. Its a Joomla/Virtmart website (PHP) so I will have a look at this & see what i can make out of it, thank you Blacknight (again). Always to my rescue.
There's a web service and a few php implementations
We've used it for a number of different things :)
 

Claudiu

New Member
What if you use the list of IPs from blockacountry to redirect with PHP? Of course, you'd have to edit the list so it's an SQL query to insert the data in the database and then you can simply use a PHP redirect. If the data from blockacountry is reliable of course...
 

unrealindeed

Administrator
Staff member
What if you use the list of IPs from blockacountry to redirect with PHP? Of course, you'd have to edit the list so it's an SQL query to insert the data in the database and then you can simply use a PHP redirect. If the data from blockacountry is reliable of course...
This is something like i thought i had to do, excluding the mysql bit, to advanced for a front end guy like me! Import/Export & running other peoples queries is as goo as i get with MySQL!
I thought i could just list the IP's in a htaccess file & throw in a 301 redirect for them!

@liner: My client is not looking for an annually fee, just a once of job that we ourselves can update if need be. (probably not an ideal solution)

Will look into maxmind and see what can be done there,

Would anyone know how to write it in the htaccess or PHP.

Something like what i got from maxmind is similar to what i trying to achieve
Code:
$country_code = apache_note("GEOIP_COUNTRY_CODE");
  if ( $country_code == "DE" )
  {
  header ("Location: http://www.google.de" );
  }  
  else
  {
  header ("Location: http://www.yoursite.com" );
  }
 

unrealindeed

Administrator
Staff member
Hi Blacknight,

We are still waiting for the client to get merchant account (uk/sterling) setup, so I havent implemented anything yet, but I think im going to run with max mind geolite country once we are ready to run. I found a tutorial online somewhere which I have bookmarked on another PC. I will post back with an update to let you know when I have it running. Thanks again to everyone for there input. Much appreciated.

Unrealindeed
 

unrealindeed

Administrator
Staff member
@Blacknight

I believe I have solved the redirect issue today, folling the below steps:

1. Download geoip.inc ((PHP API)) from MaxMind
2. Download & Unzip GeoIP.dat (GeoLite Country Database) from MaxMind.com
3. Create a folder called "geoip" and add both geoip.inc & GeoIP.dat into it.
4. Upload geoip folder to the root of your website.
5 Add the below PHP code to your index.php file.

--------------------------------------------------

require_once("geoip/geoip.inc");
$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

if($country_code == 'GB'){
header('Location: http://www.yourukwebsite.co.uk');
}

--------------------------------------------------
You can also add an elseif statement for more than one country.
--------------------------------------------------

if($country_code == 'AU'){
header('Location: http://youraustrailianwebsite.com.au');
}
elseif($country_code == 'GB'){
header('Location: http://yourukwebsite.co.uk');
}

Hope this is an easy solution for everyone.
 
Status
Not open for further replies.
Top