When you want to do an auto redirect based on the AcceptLanguage parameter sent by the browser the first solution that comes up in Google is this:
1 2 3 4 5 |
map $http_accept_language $lang { default en; ~pt pt; ~es es; } |
This will not work because the AcceptLanguage is of the following form:
1 |
Accept-Language:en-US,en;q=0.8,es;q=0.6 |
And Map will always map to the first one and will not respect the priority.
Solution:
Put this in the nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
location = / { rewrite_by_lua ' for lang in (ngx.var.http_accept_language .. ","):gmatch("([^,]*),") do if string.sub(lang, 0, 2) == "en" then ngx.redirect("/en/") end if string.sub(lang, 0, 2) == "es" then ngx.redirect("/es/") end if string.sub(lang, 0, 2) == "pt" then ngx.redirect("/pt/") end end ngx.redirect("/en/") '; #rewrite ^/$ /en/ permanent; # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location / { try_files $uri $uri/ =404; } |
make sure you do :
1 |
sudo apt-get install nginx-extras |