我有一个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]重写地图包含我们所有的传统urls当前站点上的页面和将被重定向到新站点上的等效页面的短urls (大约有4k,所以我真的需要使用地图):
Inspiration.aspx /Inspiration.aspx
Destinations/Africa/Countries/Gabon.aspx /Destinations/Africa/Gabon.aspx
indonesia /Destinations/Southeast-Asia/Indonesia.aspx问题是,在启用重写映射的情况下(即未注释掉),我所有的urls (即使是那些不匹配的urls)都会重定向到/-包括样式表、js、图像等。
我所追求的是与映射中的模式相匹配的uris,以重定向到替换和其他要通过的东西(即保持不变)。
我已经尝试在地图上设置默认值$1:
RewriteRule ^(.+)$ ${map:$1|$1} [R=301]但这只会导致重定向循环。我想我也可以跳过对所有.css、.js、.jpg等的重写,但不希望维护一个使用过的文件扩展名列表。
仅供参考,我使用的是HeliconTech的ISAPIRewrite (就像我在IIS6上一样),尽管它声称可以按照apache处理重写映射,而且我们在过去从来没有遇到过问题。
任何帮助都将不胜感激!
谢谢,亚当
发布于 2009-12-16 04:29:18
只需将实际不同的对放入映射中。否则,您将重定向到相同的值,并获得无限递归。
要仅在找到匹配项时重定向,请尝试执行以下操作:
RewriteCond ${map:$1} ^/.+
RewriteRule ^(.+)$ %0 [R=301]发布于 2016-02-01 01:06:36
这一行就是问题所在:
RewriteMap map txt:rewritemaps/map.txt [NC]不能在.htaccess文件中定义RewriteMap。(See RewriteMap docs)您必须在服务器或虚拟主机上下文中定义映射,然后才能在.htaccess文件中引用它。如果您将日志级别设置得足够高,您将看到一条警告,提示您的地图未被加载。
发布于 2010-01-07 14:57:12
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-matchedhttps://stackoverflow.com/questions/1910120
复制相似问题