问题
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