David Martirosyan
David Martirosyan

Reputation: 93

Can not use 301 redirect in NGINX

I have a web site which is translated to 18 languages. Some languages are the same (Brazilian is Portuguese). So I want to redirect from br to pt to avoid odd content, from /some_domain/br/... -> /some_domain/pt/...

I can write single redirect from one domain to another. Something like this:

location = /user/unique {
       return 301 http://www.usgreencardoffice.com/blog/the-american-dream;
    }

I want to achieve the following: domain.com/br/something -> domain.com/pt/something

But for the languages redirection, I have no idea. How can I achieve this?

Upvotes: 1

Views: 91

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

If the language code is at the beginning of the URI, a prefix location will be an efficient solution:

location ^~ /br/ {
    rewrite ^/br(.*)$ /pt$1 permanent;
}

The ^~ modifier makes this prefix location take precedence over regex locations at the same level. If you change permanent to last, the rewrite becomes internal and thus invisible to the user.

See this and this for details.

Upvotes: 1

Related Questions