views:

2156

answers:

3

Hi,

I'm using Isapi Rewrite 3 (mod rewrite clone for IIS) and trying to rewrite URLs based on the query string - and then pass on part of that query string in the rewrite.

So if I enter a URL like this: /test/home.cfm?page=default&arbitraryExtraArg=123

I want that to be rewritten as: /test/index.cfm?page=home&arbitraryExtraArg=123

I have the following condition/rule:

RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [I,R=301]

But the extra query string variables are never passed. %1 seems to be blank.

This is how to reference a pattern match from the RewriteCond, right?

What am I missing here?

Thanks!

EDIT: It looks to me like the RewriteCond is being totally ignored. Here is what my logfile looks like:

[snip] (2) init rewrite engine with requested uri /test/home.cfm?page=default
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/home.cfm'
[snip] (1) escaping /test/index.cfm?page=home 
[snip] (2) explicitly forcing redirect with http://www.devsite.com/test/index.cfm?page=home
[snip] (2) internal redirect with /test/home.cfm?page=default [INTERNAL REDIRECT]

[snip] (2) init rewrite engine with requested uri /test/index.cfm?page=home
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/index.cfm'

Should there be mention of the RewriteCond pattern check in there?

A: 

I think ISAPI Rewrite is a little different to mod_rewrite:

Whenever you put parentheses in any regular expression present in a complex rule (a rule with conditions) you are marking a submatch that could be used in a format string (using $N syntax) or as a back-reference (using \N syntax) in subsequent conditions. These submathces are global for the whole complex rule (RewriteRule directive and corresponding RewriteCond directives). Submatches are numbered from up to down and from left to right beginning from the first RewriteCond directive (if such directive exists) corresponding to the RewriteRule.

So try $1 to get the match of the first group no matter where it appears:

RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home\.cfm$ /test/index.cfm?page=home$1 [I,R=301]

ISAPI Rewrite 3 seems to be more compatible to Apache’s mod_rewrite.

Gumbo
No luck I'm afraid. I'm actually using ISAPI Rewrite 3 - just switched to it yesterday - which is why I'm reworking these rules
stubotnik
Well then I don’t know why yours doesn’t work. Or is there any other rule that might get in conflict with that one?
Gumbo
I've removed all other rules except my RewriteLog directive - still no success. I've edited the question above to include the log output - hopefully that can shed some light
stubotnik
A: 

I believe this works also:

RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]
Jack Leow
That kind of rule worked in ISAPI Rewrite 2 - but ISAPI Rewrite 3 is a clone of Apache mod rewrite. From the mod rewrite site: Note: Query StringThe Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable.
stubotnik
Ah, good to know, thanks!
Jack Leow
+2  A: 

Hi. stubotnik.

"But the extra query string variables are never passed. %1 seems to be blank." %1 is blank because according to the log the request you make is /test/home.cfm?page=default - without second parameter.

The absense of RewriteCond processing in the log may be due to low RewriteLogLevel.

And the config should be:

RewriteCond %{QUERY_STRING} ^page=default(.+)$ [NC]

RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [NC,R=301,L]

TonyCool
Brilliant stuff - thanks man!Looks like the [I] flag I was setting was causing the whole RewriteCond to be ignored. [I] - for "Ignore case" - was a hangover from IsapiRewrite version 2.
stubotnik