简体   繁体   中英

Redirect URI on NGINX

I have a page on my site (only accessible to logged in users), that looks like the following:

https://www.example.com/forum/new

However, sometimes when users click or refresh they get the page as follows:

https://www.example.com/forum%252fnew

Now %25 decodes to the % symbol, and %2f decodes to the "/", so it seems the URI is getting double-encoded.

I'm not sure how this encoding is happening, but I thought a workaround would be to have Nginx redirect back to the correct URL, with something like the following:

location ~ /forum%252Fnew {
    return 301 https://www.example.com/forum/new;
}

I have tried escaping the % in the location with \\, but neither seem to be working.

What am I missing?

The URI has been decoded and normalized before being processed by the location and rewrite directives, so the %25 looks like a single % .

The example in your question shows a regular expression location statement. The ~ operator is for case-dependent matching, whereas the ~* operator is for case-independent matching.

To make the example in your question work, you will need to change it to:

location ~* /forum%2Fnew

Or:

location ~ /forum%2fnew

See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM