1

I'm trying to use location to detect the current locale and transfer (rewrite) the request in my Nginx config file.

This is a my configuration:

location /ru 
{
    rewrite ^/ru/(.*)$ /$1?local=ru;
    rewrite ^/ru /?local=ru;
}

So, I intend to change domain.tld/ru/blog to domain.tld/blog?locale=ru.

But I have encountered one problem. As an example, the URL domain.tld/russell would interfere with this config blog.

I want to be able to get more specific for location syntax. I want to tell Nginx to run this block only and if only the URL starts with a two-letter ru path segment.

How can I do that?

0

1 Answer 1

2

To match /ru and /ru/foo but not /rufoo, you could use a regular expression location. But note that the evaluation order of regular expression locations is significant.

location ~* /ru($|/) { ... }

Alternatively, use two location blocks:

location = /ru { rewrite ^ /?local=ru; }
location /ru/ { rewrite ^/ru/(.*)$ /$1?local=ru; }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much. So, the /ru location won't match /rufoo? Because we already have that one location and it matched /rufoo too.
location /ru matches anything that begins with /ru unless a more specific location matches instead. location = /ru matches only /ru and nothing else.
Then how that would solve our problem? I mean how having two separate location blocks saves us from that problem?
Because neither of those two locations match /russell
Oh, now I saw the = sign. That makes it more specific. That is so nice. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.