首页 文章

mod_rewrite发送与重写映射不匹配的所有内容

提问于
浏览
3

我有一个htaccess文件,从常规的东西开始:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

然后有一些重写,例如:

RewriteRule ^protect/?$ /Legal/Your-Financial-Protection.aspx   [NC, L, R=301]

然后以重写映射结束:

RewriteMap map txt:rewritemaps/map.txt  [NC]
RewriteRule ^(.+)$  ${map:$1}   [R=301]

rewritemap包含我们当前网站上的所有遗留网址和短网址,这些网址将被重定向到新网站上的等效网页(大约有4k,所以我需要真正使用 Map ):

Inspiration.aspx /Inspiration.aspx
Destinations/Africa/Countries/Gabon.aspx /Destinations/Africa/Gabon.aspx
indonesia /Destinations/Southeast-Asia/Indonesia.aspx

问题是,启用重写映射(即未注释掉),我的所有网址(甚至那些不匹配的网址)都会重定向到/ - 包括样式表,js,图像等 .

我所追求的是与 Map 中的模式匹配的uris,以重定向到替换以及其他所有要传递的内容(即保持不变) .

我已尝试在 Map 上设置$ 1的默认值:

RewriteRule ^(.+)$  ${map:$1|$1}    [R=301]

但这只会导致重定向循环 . 我想我也可以跳过所有.css,.js,.jpg等的重写,但宁愿不必维护已用文件扩展名列表 .

仅供参考,我正在使用来自HeliconTech的ISAPIRewrite(因为我在IIS 6上)虽然它声称按照apache处理重写图,但我们过去从未遇到过问题 .

任何帮助将非常感谢!

谢谢,亚当

4 回答

  • 1

    adam,Gumbo的规则包括密钥中的前导斜杠,并且 Map 的发布版本缺少它们 .

    RewriteLogLevel 9对于发现诸如此类的东西非常方便 .

  • 2

    只在 Map 中放置实际不同的对 . 否则,您将重定向到相同的值并获得无限递归 .

    要仅在找到匹配项时重定向,请尝试以下操作:

    RewriteCond ${map:$1} ^/.+
    RewriteRule ^(.+)$ %0 [R=301]
    
  • 1

    这一行是问题所在:

    RewriteMap map txt:rewritemaps/map.txt  [NC]
    

    您无法在.htaccess文件中定义RewriteMap . (See RewriteMap docs)您必须在服务器或虚拟主机上下文中定义映射,然后可以在.htaccess文件中引用它 . 如果您将日志级别调高到足够高,您将看到一条警告,表明您的 Map 未加载 .

  • 1

    Gumbo 's solution is great, except that it doesn'似乎用空格来处理URL . http://www.webmasterworld.com/apache/3830228.htm提供了如何处理空间的见解,将两者结合起来如下:

    RewriteCond ${moved:${escape:$1}} ^/.+
      RewriteRule ^(.+)$ %0 [R=301]
    

    不行 . 该死的我讨厌mod_rewrite

    IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (2) init rewrite engine with requested uri /press/Partners With City.pdf
      IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (3) applying pattern '^(.+)$' to uri '/press/Partners With City.pdf'
      IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup OK: map=escape key=/press/Partners With City.pdf -> val=/press/Partners%20With%20City.pdf
      IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (6) cache lookup FAILED, forcing new map lookup
      IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup FAILED: map=moved[txt] key=/press/Partners%20With%20City.pdf
      IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (4) RewriteCond: input='' pattern='^/.+' => not-matched
    

相关问题