Sign up ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I've gotten locales up and running on a site, but would like it to automatically route to for norwegian visitors and 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.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

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


share|improve this answer
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 It returns lots of details for a given IP:

$ curl
  "ip": "",
  "hostname": "",
  "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

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('', 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];
share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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