Isapi Rewrite - access query string pattern matches in RewriteRule

stubotnik picture stubotnik · Jul 7, 2009 · Viewed 8.1k times · Source

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?

Answer

TonyCool picture TonyCool · Jul 9, 2009

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]