Rewriting URLs with Multiple Parameters

Topics: User Forum
Oct 20, 2009 at 3:38 PM

Hi Folks,

Firstly, congrats on a great rewriter - I've managed to get a basic setup running very easily :-)

My knowledge of RegEx is non-existent, so now that I've come to a point of needing to do something a little more basic, I'm well and truely stuck!

Here's a working rule I have running now;

RewriteRule ^/search/([^/]+) /results.cfm?search=$1 [L,I]
which shows as /search/myquery

Within that page (results.cfm) there is a page navigation, and I therefore need to add an extra parameter, so that the url shows as;
/search/myquery/page/1 etc.

The actual page is results.cfm?search=myquery&page=1

Browsing through the help files and discussions here, I've come up with this;

RewriteRule ^/search/([^/]+)/page/([^/]+)/$ /results.cfm?search=$1&page=$2 [L,I]

However, it basically just loads up the first page again, so isn't working for whatever reason.

If anyone could point me in the right direction, I'd be very grateful!

Oct 20, 2009 at 5:30 PM

Glad you like it!

The reason the first page loads up, when you insert a URL like /search/keyword/page/1 ,  is because you don't have a $ (line terminator) in the pattern for the first rule.  That pattern will thus match any incoming URL that begins with /search/keyword (or similar).   Once that rule matches, no other rules are evaluated, because you have the [L] modifier.  You would be able to see this very clearly in the log file - Rule 1 is matching against incoming URLs like /search/keyword/page/1 .

To match only URLs that have exactly 2 URL path segments - /search/keyword and nothing more following the query keyword - insert a $ following the capture group for the 2nd url path segment.  Like this:

RewriteRule ^/search/([^/]+)$ /results.cfm?search=$1 [L,I]

There's also a problem in your second rule. Your Regex has a trailing slash, while the incoming URL does not.

Instead of

RewriteRule ^/search/([^/]+)/page/([^/]+)/$ /results.cfm?search=$1&page=$2 [L,I]

 try this...

RewriteRule ^/search/([^/]+)/page/([^/]+)$ /results.cfm?search=$1&page=$2 [L,I]

The former matches /search/keyword/page/1/  and the latter matches /search/keyword/page/1  . 

PS: the testdriver.exe program that ships with IIRF is very good for testing URLs against your set of rules.  Also I can recommend Gerry Shaw's Regex tool for seeing regex results in real time.


Oct 20, 2009 at 5:36 PM


Thanks Cheeso, that has worked a treat.  The explanation behind it certainly helps too, I have a much better understanding of what does what now.  Much appreciated :-)