I've gotten locales up and running on a site, but would like it to automatically route to domain.com/no for norwegian visitors and domain.com/en for all other visitors. For the life of me, I seem incapable of finding a way using the built in functions.

Do I need to write a plugin to handle this or is there some clever trick I haven't thought of? Can't find anything on geolocation at all.

up vote 7 down vote accepted

The mod_geoip2 Apache module with a redirect in .htaccess file.

See: http://dev.maxmind.com/geoip/legacy/mod_geoip2/

  • This is a good idea! I'll check with my VPS host if they could enable this so I could give it a try (they're handling everything through puppet, so kinda reluctant to add stuff myself). – Broxxi Jul 13 '14 at 16:40

Doing this with mod_geoip2 on the server with a local geolocation database is definitely the best approach here, but if your not able to do that for whatever reason then an alternative is to use an API like http://ipinfo.io. It returns lots of details for a given IP:

$ curl ipinfo.io
{
  "ip": "208.54.39.191",
  "hostname": "mbf2736d0.tmodns.net",
  "city": "Los Angeles",
  "region": "California",
  "country": "US",
  "loc": "34.0522,-118.2437",
  "org": "AS21928 T-Mobile USA, Inc.",
  "postal": "90013"
}

You just need the country though, so you can speed things up a tiny bit by requesting just that:

$ curl ipinfo.io/country
US

Then you can request this information for the current user and redirect them to a different URL if they're not on the correct one. Here's what the javascript code to do that would look like:

$.get('http://ipinfo.io/country', function(country) {
    var country_is_no = country.trim() == 'NO';
    if(country_is_no and document.location.pathname != '/no') {
        document.location = '/no';
    }
    if(!country_is_no && document.location.pathname != '/en') {
        document.location = '/en';  
    }
}, "jsonp");

That only allows you to have 2 pages though (/no, and /en). That might be fine for your purposes, but an alternative would be to have no and en subdomains, and keep the rest of the content in the same place, just like wikipedia:

If you were to setup the 2 subdomains then you could extract that with the following javascript, and then redirect based on that:

var subdomain = document.location.hostname.split('.')[0];

I recently released Country Redirect, a plugin that handles this smoothly: https://superbig.co/plugins/country-redirect

It lets you map which countries get redirected to which locales, let visitors override the country, and easily let you create a locale nav based on the country mapping.

The plugin uses the same database as the accepted answer, but actually allows the user to override it, which makes for a better UX than forcing everyone into a locale.

Not exactly related but might help. You can define locales in combination with urls, so you can point no.yourdomain.com to no locale and yourdomain.com to the default locale. Will need to see some more documentation, but this might help.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.