Geolocation Click Redirection


One of the affiliate offers I participate in redirects visitors to their website to their country specific based websites which is great for the user, not so great for me because when the user is redirected the affiliate cookie is lost and I don’t get credit for the sale. Each country has their own affiliate program so it is possible to get credit for any sales that happen on those country specific sites, but the redirect has to happen before the user visits the main company website where they redirect the user.

While it is possible to create country specific landing pages for each country there is always the chance that a visitor from one of those other countries will still click on a link that would take them to the main company website. To prevent this from happening I have installed a script to check geolocation and redirect the user to the appropriate country specific website instead of them going to the main companies website and being redirected.

Eric Nagel has a PHP script you can use to do something similar, and while his method would work the thought of having to maintain the IP Geolocation database, even through a cron job, just sounds like trouble to me. Not to mention some shared web hosts don’t allow you to run cron jobs or to run a cron job that unzips a file.

The solution I came up with relies on the service Cloudflare. I wrote a post about Cloudflare earlier this year. Cloudflare has this neat little feature called Geolocation where they will add in a server variable that you can access with PHP calle $_SERVER[“HTTP_CF_IPCOUNTRY”]. Once you turn the feature on in Cloudflare all the requests to your Cloudflare enabled website will contain the country code of the user.

All you have to do then is create a PHP redirect script that checks for the Cloudflare variable and redirect the user to the correct link.

Here is a copy of what I use to redirect users.

<?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; if ($country_code=="UK") { $link = 'Insert UK Link'; } elseif ($country_code=="FR") { $link = 'Insert France Link'; } elseif ($country_code=="DE") { $link = 'Insert Germany Link'; } else { $link = 'Insert Default Link'; } header("location:$link"); exit; ?>

A word of warning, for some users in the UK their country code is returned as UK and others are GB so you might want an extra check for both codes.

I was already redirecting people through this link to help manage my links, this just adds an extra level to that redirect. It has already paid off making sure that users from the countries I specified were redirected to the correct website for them. I currently have this redirect installed on a shared server with the website added to Cloudflare.

If you need to know specific country codes Wikipedia has a list that you can use.

This is a pretty simple method and Cloudflare takes care of maintaining the IP Geolocation database for you. Of course you could do more sophisticated things than this but this gets the job done for me.

Categories: web-programming   website-monetization  
Comments