在RewriteCond中使用RewriteMap查找。 可能?

我正在尝试以下

RewriteMap lookup "txt:D:/lookup.txt" RewriteCond %{REQUEST_URI} ^/${lookup} RewriteRule ^/(.*)/(.*)$ /a/$1/b/$2.html [PT,L] 

我试图比较请求path是否与有效path开始或不。

我在查找文件中有很长的path列表。

请帮忙。

我发现你不能在RewriteCond正则expression式中有一个variables,因为它似乎只编译一次,而不是每个请求。

你可以解决这个问题,把${lookup}%{REQUEST_URI}放在一个使用分隔符的testingstring中(然后确保它们是相同的),例如:

 RewriteCond %{REQUEST_URI},/${lookup:/$1/}/$2 ^([^,]+),\1 

还要注意,对于这个给定的示例URL http://mydomain.dom/abc/mypage.html

 %{REQUEST_URI} = /abc/mypage.html 

 ${lookup:$1} = abc 

所以%{REQUEST_URI}永远不会等于${lookup:$1}

为了使他们平等的要求,你必须:

  • /来取代${lookup:$1}得到/abc/ = /${lookup:$1}/
  • 添加$2也获得所需的文件: /abc/mypage.html = /${lookup:$1}/$2
  • 因为你的密钥是/abc/ (而不是abc ),所以你需要用$1来覆盖$1来匹配键值: /${lookup:/$1/}/

所以最后你会有这个:

 RewriteMap lookup "txt:/var/www/lookup.txt" RewriteCond %{REQUEST_URI},/${lookup:/$1/}/$2 ^([^,]+),\1 RewriteRule ^/(.*)/(.*)$ /a/$1/b/$2 [R=301] 

http://mydomain.dom/abc/mypage.html给我下面的日志:

 (1) pass through /a/abc/b/mypage.html