如何阻止 RewriteRule 进入无限循环?
假设我的服务器上有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要转义
%
(百分号)符号 - 因为否则%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.
你一开始就误解了这个问题——问题不在于括号被转义。发送以获取文件系统上名为“FILE%20NAME[1].png”的文件的正确请求应为 URL 中的“FILE%2520NAME%5B1%5D.png”。
如果您只是使用了
Alias
或非重定向RewriteRule
那么循环就不会出现问题。我非常确定,如果你按照问题 1 中所说的处理,那么也不会有循环问题,因为当你发回正确的重定向目标时,新的请求 URL 将包含
%2520
(解码:%20
),与\s
不匹配,因此该规则不适用。< /p>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.
If you just used an
Alias
or a non-redirectRewriteRule
then there would be no problem with loops.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.