Automatically redirect visitors to country or city specific page

A tutorial on how to get the location from an IP address and use it to redirect your users to a region specific website.

Say you own a fancy website ( www.example.com ) that has become quite popular globally. To cater the diverse needs of your visitors, you decide to customize your website for different geographies (city / region / country). There could be various reasons to do that, for instance

  1. Redirect your website users to a country specific subdomain (or path), e.g. send all users from UK to uk.example.com or example.com/uk
  2. Set a default website for visitors from a location e.g. visitors outside US are redirected to global.example.com
  3. Translate the website in a specific language
  4. Show area specific store locations
  5. Convert product prices to another currency
  6. Customize / restrict content by countries
  7. Personalize with country flags, salutations etc.

This is where ipapi makes life easier for you. With a simple API call, you can translate the IP address of your visitor to a 2-letter country code.

Example:
If your visitor’s IP address is 8.8.8.8, a request to ipapi.co/8.8.8.8/country will return the country code for United States i.e. US

Country code detection can go on the server side or client side, depending on your architecture. Here are some code snippets in popular languages to make the integration easier for you.

Ruby

Python

PHP

Node.js

jQuery

Java

Feel free to get in touch if you need any help !

IP Lookup | IP Geolocation API | IP Address Locator by Kloudend, Inc. USA. Trusted by Fortune 500 !