Question

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?

Was it helpful?

Solution

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]

OTHER TIPS

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.

I believe this works also:

RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top