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
Wouldn't it be easier to just write search.php so that it ignored any parameters whose value was "|" ?
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
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
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)
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 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¶m2=$4 [NC] /search/¦/blah results in /search.php?param1=¶m2=blah Thanks again. (I gave you some green ) James
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.