Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My base url looks like this.


And depending on the users input it can be something like this.


The part where it says "beer" can be any string which is the actual search string the user inputs.

The part where it says "New-York" can be any city and the user has an option of setting or not setting it.

The user also has the option of setting or not setting values for the following. alcoholic can have a value of No or Yes rating can either be a value from 1 to 5 and multiple values are separated by a - price can either be a value from 1 to 5 and multiple values are separated by a - page is a number indicating the offset for pagination

I wanted to direct the url above and be able to pass the values for the actual search string ("beer"), neglect the match for the city and pass the values for alcoholic, rating, price and page if the are set.

I have something like this on my 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/

I am having a hard time matching the city part without breaking the order of the matches ($1, $2, etc).

I found this article which explains conditionals in regex patterns but i can seem to get it to work specially if page is set.

share|improve this question

migrated from Oct 9 '12 at 17:23

This question came from our site for professional and enthusiast programmers.

You question is not clear. Please try to explain exactly what you are trying to achieve. – Nir Levy Oct 9 '12 at 15:07
@NirLevy - Edited, is it clearer now? – user1423172 Oct 9 '12 at 15:50
up vote 0 down vote accepted

I don't have an apache handy to test what I am saying but you could maybe try to use non-capturing groups for the city. so they don't break your $x order

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

However, this may start to get confusing if your search term (beer) would be more then one word (red wine). My suggestion would be to use %20 or underscore for spaces in words and dashes for argument separators.

share|improve this answer
what if in the future, ill be needing to pass the city? – user1423172 Oct 10 '12 at 13:16
then you should to duplicate your set of rules and add the city to them with just plain (), where the ones with the city come before the ones without the city. And use [L] for each rule. – Nir Levy Oct 10 '12 at 13:23
cool thanks alot! – user1423172 Oct 10 '12 at 17:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.