如何阻止 RewriteRule 进入无限循环?

发布于 2024-10-04 12:37:49 字数 366 浏览 0 评论 0原文

假设我的服务器上有一个名为 FILE%20NAME[1].png 的文件。

浏览器请求 FILE%20NAME%5B1%5D.png,转义方括号,Apache 将其解释为 FILE NAME[1].png,当然找不到。

我知道我应该重命名该文件,但是如何编写重定向规则以在 .htaccess 中快速修复? 我尝试了

RewriteRule ^folder/FILE\sNAME\[1\].png /folder/FILE\%20NAME\[1\].png [R=301,QSA,L,NE]

这会导致无限循环,因为重写的文件名仍然符合规则。

Lets say I have a file named FILE%20NAME[1].png on my server.

The browser requests FILE%20NAME%5B1%5D.png, escaping the square brackets, which Apache interprets as FILE NAME[1].png and of course cannot find.

I know I should just rename the file, but how would I write a redirect rule for a quick fix in .htaccess?
I tried

RewriteRule ^folder/FILE\sNAME\[1\].png /folder/FILE\%20NAME\[1\].png [R=301,QSA,L,NE]

which results in an endless loop because the rewritten filename still matches the rule.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城花音 2024-10-11 12:37:49
RewriteRule ^folder/FILE\sNAME\[1\].png /folder/FILE\%2520NAME\[1\].png [R=301,QSA,L,NE]
RewriteRule ^folder/FILE\sNAME\[1\].png /folder/FILE\%2520NAME\[1\].png [R=301,QSA,L,NE]
沩ん囻菔务 2024-10-11 12:37:49

您需要转义 % (百分号)符号 - 因为否则 %20 会被解释为 (空格)。

% 转义为 %25,将 %20 替换为 %2520 应该可以解决问题。

另请参阅:百分比编码

You need to escape the % (percent) sign - because otherwise %20 gets interpreted as (space).

% escapes to %25, replacing %20 for %2520 should do the trick.

See also: Percent-encoding.

素年丶 2024-10-11 12:37:49
  1. 你一开始就误解了这个问题——问题不在于括号被转义。发送以获取文件系统上名为“FILE%20NAME[1].png”的文件的正确请求应为 URL 中的“FILE%2520NAME%5B1%5D.png”。

  2. 如果您只是使用了 Alias 或非重定向 RewriteRule 那么循环就不会出现问题。

  3. 我非常确定,如果你按照问题 1 中所说的处理,那么也不会有循环问题,因为当你发回正确的重定向目标时,新的请求 URL 将包含%2520(解码:%20),与 \s 不匹配,因此该规则不适用。< /p>

  1. You're mistaken about the problem to begin with -- the problem isn't that the brackets are escaped. The proper request to send to get a file named "FILE%20NAME[1].png" on the filesystem would be "FILE%2520NAME%5B1%5D.png" in the URL.

  2. If you just used an Alias or a non-redirect RewriteRule then there would be no problem with loops.

  3. I'm pretty certain that if you take care of what I said in problem 1, then there won't be a problem with loops either, because when you send back the proper redirect target, then the new request URL will contain %2520 (decoded: %20), which won't be matched by \s, and so the rule won't apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文