0

I have a site with language prefixes in $uri, for example:

Sadly, due to limitations of framework I'm using, files are also linked / looked for under prefixed addresses:

What I want my nginx to do is to try files in following order:

  1. $uri as is
  2. /files/image.png (even if en or ru or any other two-letter code was present)
  3. /index.php to get file generated if applicable

1. and 3. can be (are) done with simple:

location / {
    try_files $uri /index.php;
}

I know about ~* command to tell Nginx to do a regex match (source). Sadly, I have no idea how to formulate match to omit potential group instead of selecting it. And trying with variables before and after made me try to use if - of course I want to avoid that.

I believe I'm missing something relatively simple.

1
  • Simply, don't capture the group. What location regex did you try ? Commented Oct 16, 2014 at 21:25

1 Answer 1

1

Assuming the language part is a two lowercase characters string, this is possible to achieve this with :

location ~ "^(?:/[a-z]{2})?/(.*)$" {
    try_files /$1 /index.php;
}
3
  • You mean, as second location, defined before the / one? So simple, thank you, will try asap.
    – Mołot
    Commented Oct 17, 2014 at 5:28
  • As the first capture group is not mandatory, you don't need another location. Commented Oct 17, 2014 at 7:08
  • Thank you, with that last comment I finally think I get it :)
    – Mołot
    Commented Oct 17, 2014 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .