How to rewrite location in nginx depending on the client-browser's language?

RKI picture RKI · Sep 7, 2010 · Viewed 29.2k times · Source

How to rewrite location in nginx depending on the client-browser's language?

For example: My browser accept-language is 'uk,ru,en'. When I request location mysite.org nginx must forward to mysite.org/uk

Answer

Brian Coca picture Brian Coca · Apr 2, 2012

You can manage $language_suffix by this setting when you cannot add AcceptLanguageModule module into your system.

rewrite (.*) $1/$http_accept_language

A more resilient approach would use a map:

map $http_accept_language $lang {
        default en;
        ~es es;
        ~fr fr;
}

...

rewrite (.*) $1/$lang;