Score:1

Nginx + Wordpress language redirect wrong with default language

us flag

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;
}
Richard Smith avatar
jp flag
You have `default en;` in your map - are you saying that you don't want the default to redirect to `/en/`?
Matthew avatar
us flag
yes, @Richard Smith, just want to redirect translated language to their directories, all others to root: mydomian.com
Matthew avatar
us flag
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 avatar
us flag
I just post my whole side wide config file below, please help me check it
Matthew avatar
us flag
sorry my mistake, just deleted the reply and edited the question!
sv flag
Welcome to ServerFault. Your rewrite statement could be written like `if ( $lang != "en" ) { rewrite ^/$ /$lang/ permanent; }` in order to achieve the desired effect.
Score:0
jp flag

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.

Matthew avatar
us flag
That works but another problem comes: for other language, it redrected to mydomain.com// , there are 2 "//" in the end
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.