/ Web Design

Tutorial To Redirect Web Traffic Based On Location

Detour Sign

There's an old joke that the three most important factors in real estate is location, location, and location. That little bit of insight also holds true for websites. And the ability to deliver different content depending on the website visitor's general location is important to maximize income.

An example of this is Sympile. This website was created to promote a novel about a tech billionaire who's out to make better world (It's a great book, by the way).

The issue with the website is the book is sold on Amazon in the United States and Amazon UK in Europe.

Obviously, each Amazon site is geared to their locality. Namely, the selling prices are in their own currency.

United States Amazon

Novel Sympile Weiser United States Amazon Page

United Kingdom Amazon

So, to efficiently market the novel, it would be best if visitors to Sympile.com would be sent to the appropriate Amazon storefront based upon the visitir locality. In other words, website visitors from the UK would be sent to Amazon UK and website visitors from the United States to Amazon U.S.

This was the problem and here's the solution.

The Sympile website is served from DigitalOcean, a place where you can rent server space. I'm running Nginx on Ubuntu 14.04.

The first thing that I did was to clone the Sympile website. I then replaced the links that led to Amazon in the United States to Amazon in the United Kingdom. I established the Sympile U.K. website under a subdomain: eu.sympile.com..

Bottom line is I have two sites ready to go: Sympile-US and Sympile-UK. Now, to push Sympile visitors to the correct site.

Nginx has a module called GEOIP. This module "creates variables with values depending on the client IP address, using the precompiled MaxMind databases."

To put into English this explanation from Nginx, the GEOIP module uses an IP database to determine which country the website visitor is from. Then an "if statement" is inserted into the appropriate sites-available file that will direct UK visitors to eu.sympile.com. All other visitors will default to the United States.


The next few steps are from howtoforge.

  • Determine if the GEOIP module is installed on your version of Nginx.

$ nginx -V

If --with-http-geoip-module is returned among the many modules, you are good to go. If not, you'll have to install the GEOIP module.

  • Assuming the GEOIP module is installed, you need to create a directory to store the IP database that will be downloaded.

$ sudo mkdir /etc/nginx/geoip

  • Change into this directory

$ cd /etc/nginx/geoip

  • Download from Maxmind the IP databases into this directory.

$ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

$ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

  • Unzip the databases

$ sudo gunzip GeoIP.dat.gz

$ sudo gunzip GeoLiteCity.dat.gz

  • Edit the nginx.conf file. I use vim. You can use any editor you like. I also like to make a backup of the nginx.conf before I touch it.

$ cd /etc/nginx/

$ sudo cp nginx.conf nginxdate.removeconf

$ sudo vim nginx.conf

At the end of the http section, add in:

geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database

  • Save and close.

  • Check that the nginx configuration files are working.

$ sudo nginx -t

This should return "test is successful."

  • Now, you must edit the applicable "sites-available" file.

$ cd /etc/nginx/sites-available

$ sudo vim example_sites_available

In the server section, add in the following:

if ($geoip_country_code ~ (GB|IE|IM)) { return 301 https://new_site.com;

Substitute whatever country codes you like for GB, IE, or IM. You can get the country codes here.

  • Retest the nginx configuration.

$ sudo nginx -t

  • Restart the Nginx server

$ sudo service nginx restart

That's it.

To test, go to locabrowser to browse your subject website from a proxy server that is in the country you want to redirect.

You should see the correct website for each country.

If you have any questions, comments, corrections, suggestions, please drop me an email.