Mod Rewrite Not Performing Multiple RewriteMap Lookups

元气小坏坏 提交于 2019-12-12 01:43:10

问题


My Rewrite rule is not performing multiple rewritemap lookups and I am confused why. I am trying to redirect my users to a new URL search structure. The rewrite rule does not work when there are multiple search parameters and I am unsure why.

// 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

Output:

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

My first output is being mapped correctly, but my second one is not. Apache has not performed any mapping. Any reason why this is happening?


回答1:


Solution is to put the mapping lookup within the Rewrite rule:

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]


来源:https://stackoverflow.com/questions/39398815/mod-rewrite-not-performing-multiple-rewritemap-lookups

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!