开发者

How do I stop RewriteRule from going into an endless loop?

开发者 https://www.devze.com 2023-01-27 09:27 出处:网络
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 cann

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.


  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消