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

时间:2009-12-15 20:20:42

标签: .htaccess mod-rewrite rewritemap

我有一个以常规内容开头的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,所以我需要真正使用地图):

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

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

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

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

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

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

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

非常感谢任何帮助!

谢谢,亚当

4 个答案:

答案 0 :(得分:2)

这一行是问题所在:

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

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

Gumbo的解决方案很棒,除了它似乎没有用空格处理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

答案 3 :(得分:1)

adam,Gumbo的规则包括密钥中的主要斜线, 并且发布的地图版本缺少它们。

RewriteLogLevel 9非常便于发现此类内容。