I wonder how to redirect traffic based on browser language using NGINX without changing the URL.
Here is my file structure:
en
|- index.html
fr
|- index.html
I followed this tutorial, which is working as expected e.g. /home
is redirected to /en/home
.
Now is it possible to do the same without changing the address bar URL?
Here is my NGINX config:
server {
listen 80;
server_name localhost;
location /en/ {
alias /usr/share/nginx/html/en/;
try_files $uri$args $uri$args/ /en/index.html;
}
location /fr/ {
alias /usr/share/nginx/html/fr/;
try_files $uri$args $uri$args/ /fr/index.html;
}
set $first_language $http_accept_language;
if ($http_accept_language ~* '^(.+?),') {
set $first_language $1;
}
set $language_suffix 'en';
if ($first_language ~* 'fr') {
set $language_suffix 'fr';
}
location / {
rewrite ^/$ /$language_suffix/index.html permanent;
}
}