Automatically redirect visitors to country specific page

ipapi ~
Mar 20, 2017 · 2 min read

- A tutorial on how to get the country from an IP address

Say you own a fancy website ( ) that has become quite popular globally. So, to cater the diverse needs of your visitors, you decide to customize your website for different countries. There could be various reasons to do that, for instance

  1. redirect visitors to a country specific subdomain (or path)
    e.g. or
  2. translate the website in a specific language
  3. show country specific store locations
  4. convert product prices to another currency
  5. customize / restrict content by countries
  6. 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.


require 'net/http'

puts Net::HTTP.get(URI(''))


from requests import get

print get('').text


echo file_get_contents('');


var https = require('https');

https.get('', function(resp){
var body = ''
resp.on('data', function(data){
body += data;

resp.on('end', function(){


$.get('', function(data){



public class Ipapi {

public static void main(String[] args) throws IOException {
URL ipapi = new URL("");

URLConnection c = ipapi.openConnection();
c.setRequestProperty("User-Agent", "java-ipapi-client");
BufferedReader reader = new BufferedReader(
new InputStreamReader(c.getInputStream())
String location = reader.readLine();



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

ipapi ~

Written by

IP Lookup | IP Location | Geolocation API — Best IP Address Finder —

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade