1

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.

2
  • You question is not clear. Please try to explain exactly what you are trying to achieve.
    – Nir Levy
    Oct 9, 2012 at 15:07
  • @NirLevy - Edited, is it clearer now?
    – user1423172
    Oct 9, 2012 at 15:50

1 Answer 1

0

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.

2
  • what if in the future, ill be needing to pass the city? Oct 10, 2012 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, 2012 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .