Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My base url looks like this.

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

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

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

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 stackoverflow.com 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

1 Answer 1

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

 
discard

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.