【问题标题】:How can I apply an htaccess rewrite rule to a URL containing a linefeed character (%0A)?如何将 htaccess 重写规则应用于包含换行符 (%0A) 的 URL?
【发布时间】:2012-02-03 18:51:09
【问题描述】:

我遇到了包含以下格式的转义换行符的错误 URL:

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

但是,即使我尝试了最全局的重写...

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

...Apache 仍然抛出 404:

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

(Note the space.)

如何优雅地拦截这些不良 URL 并将它们路由到正确的目的地?

【问题讨论】:

  • 你有没有得到这个问题的答案?

标签: apache .htaccess url rewrite


【解决方案1】:

我遇到了同样的问题。我在 sarumont 的回答的帮助下修复了它。

示例网址,可在网站管理员工具中找到:

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

我在 Apache 配置中添加的重写规则:

RewriteRule ^/dummy-url/dummy-[\n\r]+full-version-download$ /dummy-url/dummy-full-version-download [L,R=301]

【讨论】:

  • 这是正确答案。 [\n\r] 字符是必需的,因为诸如 .+ 之类的通配符会导致 400 错误请求。
【解决方案2】:

^.*$ 不会跨越换行符。试试普通的.*。或者,尝试匹配换行符:[\r\n]

【讨论】:

    【解决方案3】:

    \s 添加到 RewriteRule 应该可以修复它。

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

    更具体地说,作为一个包罗万象

    RewriteRule ^\s(.*)$ http://www.example.com/$1 [R=301,L]
    

    【讨论】:

      【解决方案4】:

      这是一个非常古老的问题,但我认为没有一个答案是正确的,所以发布一个答案:

      用这个替换你的规则:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-08
        • 2013-08-27
        • 1970-01-01
        • 2011-12-06
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多