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

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

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

<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.

MSc. Computer Engineer. Founder of @image4io, .NET Software Consultant @kloia_com, co-organizer in @serverlesstr, Community Builder of @awscloud.

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