0

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;
  }
}
Maxime Gélinas
  • 2,202
  • 2
  • 18
  • 35

1 Answers1

0

Change the file structure to (en as default locale):

 index.html
 fr
|-  index.html

It works on Node.js localhost for me, but I have not try it on production server.