简体   繁体   English

NGINX 上的重定向 URI

[英]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 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 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.现在 %25 解码为 % 符号,而 %2f 解码为“/”,因此 URI 似乎被双重编码。

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:我不确定这种编码是如何发生的,但我认为一种解决方法是让 Nginx 重定向回正确的 URL,如下所示:

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 % .在被locationrewrite指令处理之前,URI 已经被解码和规范化,所以%25看起来像一个%

The example in your question shows a regular expression location statement.您问题中的示例显示了正则表达式location语句。 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.有关详细信息,请参阅此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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