Plea for help - country based redirect

software development

#1

Hi,

I’ve been trying for ages to set up a simple country-based redirect of a page, but I’m failing badly. Would any kind sole out there have any idea what I am doing wrong?

I would be really, hugely grateful for any help…

Basically, I would like to redirect visitors from the UK to one page, visitors from the US to a different page and all other visitors to a third page

I followed the only instructions I could find on the web for dreamhost, using shell_exec and geoip-lookup, shown below, but am failing to get the page to redirect

I can get it to recognise the country and echo the correct geoip country code, but I just can’t get the page to redirect

[php]<?php
$output = shell_exec('geoip-lookup '.$_SERVER[‘REMOTE_ADDR’]);
echo($output);
if($output == ‘GB’)
{
header(“HTTP/1.1 301 Moved Permanently”);
header(‘Location: http://www.mysite.com/uk/’);
}
if($output == ‘US’)
{
header(“HTTP/1.1 301 Moved Permanently”);
header(‘Location: http://www.mysite.com/us/’);
}
else {
header(“HTTP/1.1 301 Moved Permanently”);
header(‘Location: http://www.mysite.com/other/’);
}
?>[/php]


#2

header() must precede all other functions that generate output. Lose the echo() and it’ll work.


#3

Thanks for that, I removed the echo() but it still doesn’t work

I must have made another mistake on top of leaving the echo function in there…


#4

Well, I finally figured out a way of getting country-based redirects to work on my site, although I had to use a different method

I found I could get the dreamhost database (using shell_exec etc.) to work fine as far as using the echo function goes, but could not get it to work when attempting to redirect based on the geoip country codes

So, I decided to upload the database and geoip inc file for myself, following the exact protocol on the url shown below. It then worked just fine and first time:

Cheers