1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mod_cond/mod_rewrite help needed

Discussion in 'Apache' started by Liminal, Aug 5, 2005.

  1. #1
    Hello all,

    I am having a hard time writing the following rule

    Part 1 (easy)
    If user requests /search/<keywords>/<category>/<sortby>/<sorttype> where keywords is an actual string, the following should be invoked: /search.php?keyword=<keywords>&category=<category>&sortby=<sortby>&sorttype=<sorttype>


    Part 2 (not-so-easy)
    If user requests /search/<keywords>/<category>/<sortby>/<sorttype> where "|" is listed as any of the params (meaning no filtering for that param is needed), do not use that param in the search filter. It would be nice to be able to exclude such params from the query string (i.e. /search.php?keyword=seo&category=webmaster when the original search contained /search/seo/webmaster/|/| -- only <keywords> and <category> are supplied )


    I believe it is a combination of RewriteCond and RewriteRule that needs to be applied but have no experience of dealing with the former.

    Any help will be greatly appreciated!
    James
     
    Liminal, Aug 5, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Wouldn't it be easier to just write search.php so that it ignored any parameters whose value was "|" ?
     
    johnt, Aug 5, 2005 IP
  3. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You know I was trying to avoid it but I guess I might just have to resort to it..

    Thanks for pointing out the viability of that option ;)
     
    Liminal, Aug 5, 2005 IP
  4. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's the cleanest way that I can think of doing it.

    The only way I can think of to do it in mod_rewrite would be to test for each possible combination of | or text for each variables, sort of

    variable variable variable |
    variable variable | variable

    etc.

    Even with only 4 sections in the query string that's still 16 different combinations, and if you add any more later its just going to get uglier.

    Or maybe I'm missing something blindingly obvious because it's Friday afternoon :)
     
    johnt, Aug 5, 2005 IP
  5. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I agree. This seems to be the way to go. I would love to know how to opimize it a bit by converting all "|" to "" within RewriteRule so that the the search script can stay unchanged (it's ok to pass empty param values)
     
    Liminal, Aug 5, 2005 IP
  6. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I wonder if something like the following would work

    RewriteRule /search/(.*)/|(.*) $1//$2 [next]
    RewriteCond /search/([a-zA-Z0-9]*)/([a-zA-Z0-9 ]*)/([a-zA-Z0-9 ]*)/([a-zA-Z0-9 ]*) /search.php?keyword=$1&category=$2&sortby=$3&sorttype=$4 [L]

    In theory it should strip out a | if it exists, then look for the next pipe etc until there aren't any left.
    Like I said, in theory ;)
     
    johnt, Aug 5, 2005 IP
    Liminal likes this.
  7. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #7
    johnt

    I looked at your suggestion and could see where you were going but I don't think it would do the trick.

    If you are curious, this is what i came up with. For simplicity, I am dealing with 2 params only

    ^/search/([\¦]?)([a-z]*)/([\¦]?)([a-z]*)/?$ /search.php?param1=$2&param2=$4 [NC]

    /search/¦/blah
    results in
    /search.php?param1=&param2=blah

    Thanks again. (I gave you some green ;))

    James
     
    Liminal, Aug 5, 2005 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    If you're trying to for example keep

    &sortby=<sortby>&sorttype=<sorttype>

    out of the URL when not used, the only way to do that is to make the script not put them in links. All mod_rewrite does is makes the changed URLs work, it doesn't effect how URLs show up in links! So you have to edit the script.
     
    Nintendo, Aug 5, 2005 IP