According to RFC3986 URL like this


converted by some UserAgent (curl would be nice example) to http://example.com/catalog/category/brand/product.html

How can I achieve such behavior using nginx rewrite, if my stupid UserAgent can not parse original URL in correct way?

Is it ever possible or not?


You can replace /./ sequences and /goods/../ sequences with a single /. For example:

rewrite ^(.*)/\./(.*)$ $1/$2 redirect;
rewrite ^(.*)/(?!\.\.)[^/]+/\.\./(.*)$ $1/$2 redirect;

The /(?!\.\.)[^/]+/ sequence should match any path element that is not another ... The above rewrites are recursive and issue 302 responses until all of the /./ and /../ sequences are correctly removed.

Alternatively, the same can be achieved using an internal redirect, by placing rewrite...last statements within a suitable location block. For example:

location / {
    rewrite ^(.*)/\./(.*)$ $1/$2 last;
    rewrite ^(.*)/(?!\.\.)[^/]+/\.\./(.*)$ $1/$2 last;

In this case, the URL displayed on the browser's address bar will not be changed.

See this document for details of the rewrite directive, and this link for details of the negative lookahead assertion.

  • Thanks, I'll try to use the first rules given by you. I'm familiar enough with simple nginx rewrite rules, but tight stockage of slashes and dots makes me stuck and a little bit sick – Twissell May 4 '18 at 15:58

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.