Log in

No account? Create an account
24 January 2012 @ 12:26 am
Localization for linking to the Amazon Kindle store  
Amazon, somewhat annoyingly, has different pages for buying my ebook depending on what country you're in.  I checked using a proxy, and it appears that if I just link to the US page for my site, UK visitors are in for a frustrating experience.  Fortunately, once you've set up all the different pages on Amazon, all you need to do is detect a visitor's country of origin using their IP address, and then redirect them to the appropriate page.  Here's how I did it:

$country = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR']);
switch($country) {
case 'IE':
case 'GB': $amazonDomain = 'co.uk'; break;
case 'AT':
case 'DE': $amazonDomain = 'de';break;
case 'PT':
case 'ES': $amazonDomain = 'es';break;
case 'FR': $amazonDomain = 'fr';break;
case 'IT': $amazonDomain = 'it';break;
default: $amazonDomain = 'com';break;
header('Location: https://www.amazon.'.$amazonDomain.'/dp/B0061Z9KWM?tag=hamlandthephi-20');

Change the book ID and affiliate link to your own (or just remove the tag, since Amazon doesn't pay affiliate money for kindle sales right now anyway), then paste it into a page all on its own.  Links to that page will then be automatically redirected to the right place.