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 ( ) 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 or
  2. Set a default website for visitors from a location e.g. visitors outside US are redirected to
  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.

If your visitor’s IP address is, a request to 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.







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 !