htaccess重写规则以匹配dynamic参数

我的基地url是这样的。

http://localhost/test/s/beer-drinks 

而根据用户的input,可以是这样的。

 http://localhost/test/s/beer-New-York-drinks_alcoholic-No_rating-2-3_price-1-3_page-2 

它所说的“啤酒”的部分可以是用户input的实际searchstring的任何string。

“New-York”的部分可以是任何城市,用户可以select设置或不设置。

用户还可以select是否设置以下值。 酒精可以具有“否”或“是”的值可以是从1到5的值,并且多个值由 – 价格分隔可以是从1到5的值并且多个值由 – 页分隔是指示分页的偏移量

我想直接上面的url,并能够传递实际searchstring(“啤酒”)的值,忽略匹配的城市,并通过酒精,评级,价格和页面的价值,如果设置。

我在我的htaccess上有这样的东西

 RewriteRule ^search$ s/ [R=301,L] RewriteRule ^search/$ s/ [R=301,L] RewriteRule ^s/(.*)-drinks_alcoholic-(.*)_rating-(.*)_price-(.*)_page-(.*) search/q_$1/alcoholic_$2/rating_$3/price_$4/page_$5/ . . . RewriteRule ^s/(.*)-drinks_page-(.*) search/q_$1/page_$2/ RewriteRule ^s/(.*)-drinks search/q_$1/ 

我很难匹配城市部分,而不会打破比赛的顺序($ 1,$ 2等)。

我发现这篇文章解释了正则expression式中的条件,但是如果页面被设置,我似乎可以得到它的工作。

我没有apache方便testing我在说什么,但你也许可以尝试使用城市的非捕获组。 所以他们不会破坏你的$x订单

 RewriteRule ^s/(.*)(?:-[A-Za-z-]+)?-drinks_alcoholic-(.*)_rating-(.*)_price-(.*)_page-(.*) search/q_$1/alcoholic_$2/rating_$3/price_$4/page_$5/ #( - start group #?: - make it non-capturing, which means it will not effect $1,$2... #- - if the group starts with - #[A-Za-z-]+ - and looks like a words seperated by dashes (maybe you should enhance this a bit) #)? - and the entire group may appear or not 

但是,如果您的search词(啤酒)超过一个单词(红酒),这可能会开始混乱。 我的build议是将%20或下划线用作参数分隔符的单词和破折号空格。