Mod重写不执行多个RewriteMap查找

时间:2016-09-08 19:30:37

标签: mod-rewrite

我的重写规则没有执行多次重写映射查找,我很困惑。我正在尝试将用户重定向到新的URL搜索结构。当有多个搜索参数时,重写规则不起作用,我不确定原因。

// Desired mapping examples:
http://www.host.com/search/small         => http://www.host.com/search?q=tall
http://www.host.com/search/medium/brown  => http://www.host.com/search?q=grande,chocolate

// Rule
RewriteMap searchMap txt:/opt/etc/apache/conf/searchMap.txt

// 1 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)
RewriteRule "/search/(([^/]*))$" "http://%{HTTP_HOST}/search?q=%1" [NC,R,L]

// 2 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)/([^/]*)
RewriteRule "/search/(([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=%1,%2" [NC,R,L]

// 3 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)/([^/]*)/([^/]*)
RewriteRule "/search/(([^/]*)/([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=%1,%2,%3" [NC,R,L]

// searchMap.txt
small tall
medium grande
low-fat healthy
low-calorie healthy
brown chocolate
pink strawberry

输出:

http://www.host.com/search/small         => http://www.host.com/search?q=tall
http://www.host.com/search/small/brown   => http://www.host.com/search?q=small,brown

我的第一个输出正确映射,但我的第二个输出没有。 Apache没有执行任何映射。出现这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

解决方案是将映射查找放在重写规则中:

RewriteMap searchMap txt:/opt/etc/apache/conf/searchMap.txt
RewriteRule "/search/(([^/]*))$" "http://%{HTTP_HOST}/search?q=${searchMap:$1|$1}" [NC,R,L]
RewriteRule "/search/(([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=${searchMap:$1|$1},${searchMap:$2|$2}" [NC,R,L]