1

Nginx + Wordpress language redirect wrong with default language

Sorry for duplicate this questions here: Nginx + Wordpress language redirect

All works but for the default EN language.

I just want to redirect JP visitors to /ja/ directory, maybe more other countries to other directories later.

For now, I want all other countries to the root domain.

The rule I used is:

map $http_accept_language $lang {
    default en;
    ~ja ja;
}

...

rewrite ^/$ /$lang/ permanent;

This rule redirected all other visitors to mydomain.com/en/, that's 404 of course since EN is my main language!

=======================================

This is all the rules in the site side config

Hope anyone can help me with this !

map $http_accept_language $lang {
default en;
~ja ja;
}

server 
{ 
listen 80; 
listen 443 ssl http2; 
server_name mydomain.com www.mydomain.com; 
index index.php index.html index.htm default.php default.htm default.html; 
root /www/wwwroot/mydomain.com;

#REWRITE-START

rewrite ^/$ /$lang/ permanent;

include /www/server/panel/vhost/rewrite/mydomain.com.conf;
#REWRITE-END


location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
    return 404;
}


location ~ \.well-known{
    allow all;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires      30d;
    error_log /dev/null;
    access_log /dev/null;
}

location ~ .*\.(js|css)?$
{
    expires      12h;
    error_log /dev/null;
    access_log /dev/null; 
}
access_log  /www/wwwlogs/mydomain.com.log;
error_log  /www/wwwlogs/mydomain.com.log;
}
Matthew
  • 11
  • 3
  • You have `default en;` in your map - are you saying that you don't want the default to redirect to `/en/`? – Richard Smith Jul 07 '21 at 18:39
  • yes, @Richard Smith, just want to redirect translated language to their directories, all others to root: mydomian.com – Matthew Jul 07 '21 at 18:44
  • Hi @Richard Smith , thanks for your reply, but sorry since I don't know how to code, so would you please help me with the ` if ($lang) { ... }` – Matthew Jul 07 '21 at 18:55
  • I just post my whole side wide config file below, please help me check it – Matthew Jul 07 '21 at 19:07
  • sorry my mistake, just deleted the reply and edited the question! – Matthew Jul 07 '21 at 19:44
  • Welcome to ServerFault. Your rewrite statement could be written like `if ( $lang != "en" ) { rewrite ^/$ /$lang/ permanent; }` in order to achieve the desired effect. – Pothi Kalimuthu Jul 08 '21 at 03:16

1 Answers1

0

If you remove the default en; line from your map, the value of $lang will become the empty string which is evaluated as false in an if expression. See this document for details.

You can replace the rewrite ^/$ ... statement with a location = / block. See this document for details.

For example:

map $http_accept_language $lang {
    ~ja ja;
}

server {
    ...
    location = / {
        if ($lang) {
            return 301 /$lang/$is_args$args;
        }
    }
    ...
}

See this document on which statements are permitted within an if block that's nested within a location block.

Richard Smith
  • 11,859
  • 2
  • 18
  • 26
  • That works but another problem comes: for other language, it redrected to mydomain.com// , there are 2 "//" in the end – Matthew Jul 07 '21 at 20:09