How do I stop RewriteRule from going into an endless loop?
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 file开发者_JAVA百科name still matches the rule.
RewriteRule ^folder/FILE\sNAME\[1\].png /folder/FILE\%2520NAME\[1\].png [R=301,QSA,L,NE]
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.
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.
精彩评论