Dan P.
Dan P.

Reputation: 1775

nginx location rule interference

I have this Nginx rule:

location /fr {
  rewrite ^/(fr)(?:/([^/]+))?$ /$2?lang=$1 last;
}

I have pages such as:

example.com/en/some-page

example.com/fr/some-page

example.com/fr

...

But I also have a page which starts by fr:

example.com/free-plan // doesn't work

example.com/fr/free-plan // doesn't work either

How can I modify my Nginx rule so that the /fr rule doesn't interference with my free-plan page?

Ps: I use PHP and rules to service page without the extension:

location / {
  try_files $uri $uri.html $uri/ @extensionless-php;
}

location @extensionless-php {
  rewrite ^(.*)$ $1.php last;
}

Upvotes: 1

Views: 59

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

I would suggest that you treat /fr as a special case and use location /fr/ to avoid side-effects from pages that begin with fr.

For example:

location = /fr {
    rewrite ^/(.*)$ /?lang=$1 last;
}
location /fr/ {
    ...
}

See this document for details.

Upvotes: 1

Related Questions