How can I apply an htaccess rewrite rule to a URL

2019-04-14 12:05发布

I'm running into bad URLs that contain an escaped line-feed in the following format:

http://domain.com/%0Apath/to/file.txt

However, even if I try the most global rewrite possible...

RewriteRule ^.*$ /path/to/file.txt [R=301,L]

...Apache still throws a 404:

The requested URL / path/to/file.txt was not found on this server.

(Note the space.)

How can I gracefully intercept these bad URLs and route them to the right destination?

4条回答
混吃等死
2楼-- · 2019-04-14 12:44

This is very old question but I think none of the answer are right so posting an answer:

Replace your rule with this:

RewriteRule .* /path/to/file.txt [R=301,L]
查看更多
淡お忘
3楼-- · 2019-04-14 12:52

^.*$ won't span the linefeed. Try a plain .*. Alternatively, try matching on the newline characters: [\r\n].

查看更多
Lonely孤独者°
4楼-- · 2019-04-14 12:56

I had the same Problem. I fixed it with the help of sarumont's answer.

Example URL, found in Webmaster Tools:

/dummy-url/dummy-%0A%0Afull-version-download

Rewrite Rule that I added in Apache config:

RewriteRule ^/dummy-url/dummy-[\n\r]+full-version-download$ /dummy-url/dummy-full-version-download [L,R=301]
查看更多
\"骚年 ilove
5楼-- · 2019-04-14 13:03

Adding \s to the RewriteRule should fix it.

RewriteRule ^\s.*$ /path/to/file.txt [R=301,L]

More specifically as a catch-all

RewriteRule ^\s(.*)$ http://www.example.com/$1 [R=301,L]
查看更多
登录 后发表回答