How to redirect a website according to country's IP address

2.4k views Asked by At

I'm working on a messenger app whose server side code is developed in Erlang.

The problem which I'm facing is regarding redirection of website according to country specific domain.

For example: when user's types google.co in message box, it automatically displays google.co.uk, how can I redirect it to google.co.in if I'm in India?

For finding country's location, I found this library on github: https://github.com/mochi/egeoip

How can I use this geoLocation for redirecting to particular country specific website?

ScreenShot, when I entered facebook.com, it automatically displays preview in my local language.

But in case of my app, it shows preview in some foreign language, russian maybe.

1

There are 1 answers

0
Ruel On

I've read the comments, and since you are not considering having datasets as an option, I think what you may want to do is something like this:

First thing to understand is how those previews work. In any (popular) messaging app, if you type in a URL, the app will send a request to the URL and get the website metadata. Then it will be displayed in the UI.

The country detection, is a bit more complicated and done in a variety of ways. But thankfully, you (almost) don't have to do anything. This is a rather long topic, but I'll try to shorten it out.


Text Localization

In some websites (might be the case of Facebook's in your example), they do country detection on the application layer, and then based on that country, it will use a specific language for the website's text. This all usually happens before the website renders it's content, so you do not have to worry about it.

GeoDNS

This one occurs on the DNS layer, and probably the most popular. Domain names can be assigned a handful of IP addresses. These IPs can point to different versions of the website, and in the case of GeoDNS it will be up to the DNS manager to assign a country to an IP. So when a DNS query came from Russia, the requesting IP's country will be resolved and then the IP assigned to it (if any) will be returned. This is used by websites especially for country-specific features or content. Best example is Netflix.

Redirects

In case of Google redirecting you to a different domain, this might be how they do it. Country is being resolved via the IP address in the application (HTTP) layer, and then does a 301/302 redirect, pointing to the new domain name. This one, you may need to do something on. So given that your application needs to do an HTTP request to the URL the user has entered, if it returns a redirect, you must follow it. Many HTTP libs/clients already does this, but on some you might have to explicitly turn on the option to follow redirects.


One important thing to note is to do the HTTP request on the client side. Otherwise, you will be resolving to the same country (where your server resides) regardless of where your user is.