Visitor (User) Routing by Geolocation

When I was planing to build this blog I wanted it to be multilingual (at least bilingual) and I search for a blogging framework to that but every one had some downsides. I liked Ghost.org most, so I was looking for way to make Ghost.org multilingual.

After giving some thought about it, I find a way. I did it and shared it in this post. Set Up Multilingual Ghost.org Blog

But there is one minor problem. I set up two blogs. One for Turkish and another for English. Their addresses are ‘irensaltali.com/tr’ and ‘irensaltali.com/en’. So I need a way to redirect visitor from irensaltali.com to ‘/tr’ or ‘/en’ depending on their location.

geoPlugin

When searching for way to detect visitors country I come across with geoPlugin. It is so beautiful plugin to use. It gives you very good information to localize website according to incoming visitor. You can check what information it can get from you by clicking this.

So I did use this plugin as you can see below:

<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script><script>
var country = "";
country =geoplugin_countryName();
//alert("Welcome to our visitors from "+country);
if (country.indexOf('Turkey') <= -1) {
document.location.href = 'https://irensaltali.com/en';
}
else {
document.location.href = 'https://irensaltali.com/tr';
}
</script>

Adblocker

These God damn adblockers. No, just kidding. I like adblocker and use it. But some adblocker filters block geoPlugin script. So I needed to think a way if it happens.

I decided if geoPlugin is blocked it’ll redirect according to visitor’s browser language. And here is how:

<script>
var language = navigator.language || navigator.browserLanguage;
//alert("Your browser language is "+language);
if (language.indexOf('en') > -1) {
document.location.href = 'https://irensaltali.com/en';
}
else {
document.location.href = 'https://irensaltali.com/tr';
}
</script>

Combine

I prefer geolocation based redirecting, so I combine these two methods in favor of geolocation based routing.

<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script><script>
try{
var country = "";
country =geoplugin_countryName();
//alert("Welcome to our visitors from "+country);
if (country.indexOf('Turkey') <= -1) {
document.location.href = 'https://irensaltali.com/en';
}
else {
document.location.href = 'https://irensaltali.com/tr';
}
}
catch(err){
var language = navigator.language || navigator.browserLanguage;
//alert("Your browser language is "+language);
if (language.indexOf('en') > -1) {
document.location.href = 'https://irensaltali.com/en';
}
else {
document.location.href = 'https://irensaltali.com/tr';
}
}
</script>

That’s it.

I hope this post has helped you.

Originally published at irensaltali.com on February 9, 2018.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store