<p>I recently released Country Redirect, a plugin that handles this smoothly: <a href="https://superbig.co/plugins/country-redirect" rel="nofollow noreferrer">https://superbig.co/plugins/country-redirect</a></p>

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

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

CC BY-SA 3.0
2017-04-18T13:19:24.480Z
2017-04-26T19:23:55.457Z
2017-04-26T19:23:55.457Z
Fred Carlsen
<p>Doing this with <code>mod_geoip2</code> 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 <a href="http://ipinfo.io" rel="nofollow">http://ipinfo.io</a>. It returns lots of details for a given IP:</p>

<pre><code>$ 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"
}
</code></pre>

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

<pre><code>$ curl ipinfo.io/country
US
</code></pre>

<p>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:</p>

<pre><code>$.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 &amp;&amp; document.location.pathname != '/en') {
        document.location = '/en';  
    }
}, "jsonp");
</code></pre>

<p>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:</p>

<ul>
<li><a href="https://en.wikipedia.org/wiki/Wikipedia" rel="nofollow">https://en.wikipedia.org/wiki/Wikipedia</a></li>
<li><a href="https://no.wikipedia.org/wiki/Wikipedia" rel="nofollow">https://no.wikipedia.org/wiki/Wikipedia</a></li>
</ul>

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

<pre><code>var subdomain = document.location.hostname.split('.')[0];
</code></pre>

CC BY-SA 3.0
2015-08-25T02:54:27.747Z
2015-08-25T02:54:27.747Z
Ben Dowling
<p>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.</p>

CC BY-SA 3.0
2014-07-13T15:43:31.100Z
2014-07-13T15:43:31.100Z
mbalparda
<p>The <em>mod_geoip2</em> Apache module with a redirect in .htaccess file.</p>

<p>See: <a href="http://dev.maxmind.com/geoip/legacy/mod_geoip2/">http://dev.maxmind.com/geoip/legacy/mod_geoip2/</a></p>

CC BY-SA 3.0
2014-07-13T16:40:13.227Z
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
CC BY-SA 3.0
2014-07-13T16:38:45.427Z
2014-07-13T16:38:45.427Z
Andrea DeMers
<p>I've gotten locales up and running on a site, but would like it to automatically route to <code>domain.com/no</code> for norwegian visitors and <code>domain.com/en</code> for all other visitors. For the life of me, I seem incapable of finding a way using the built in functions. </p>

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

<localization>
Locale change based on geolocation
# Locale change based on geolocation

(Asked by: username_0 on 2014-07-13)

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.

**Tags:** localization

---

## 4 answers

### Answer by username_4 on 2014-07-13. Score: 7 | Accepted answer

The *mod\_geoip2* Apache module with a redirect in .htaccess file.

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

> **Comments:**
> **username_0**: 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).  

---

### Answer by username_1 on 2017-04-18. Score: 4

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.


---

### Answer by username_2 on 2015-08-25. Score: 3

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:

https://en.wikipedia.org/wiki/Wikipedia
https://no.wikipedia.org/wiki/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];


---

### Answer by username_3 on 2014-07-13. Score: 2

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.


---

