Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Query String to IIS Rewrite Map

I have a ReWrite Map and I would like to append any query parameters in the requested URL to the rewritten URL.

For instance:

  • /page/abc/ ---> /index.cfm?page=abc (works)
  • /page/abc/?param1=111 ---> /index.cfm?page=abc&param1=111 (doesn't work)
  • /page/abc/?param3=333&param4=444 ---> /index.cfm?page=abc&param3=333&param4=444 (doesn't work)

My web.config is:

[...]
<rules>
    <clear />
    <rule name="Rewrite rule1 for SiteMapEngine">
        <match url=".*" />
        <conditions>
            <add input="{SiteMapEngine:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Rewrite" url="{C:1}" appendQueryString="true" />
    </rule>
</rules>
[...]
like image 321
Philip P. Avatar asked Jan 16 '11 23:01

Philip P.


3 Answers

I'll be damned if I can find a reference for this, but it's my understanding that in some versions of IIS {REQUEST_URI} comes back without it's query string, and will be empty entirely if rewriting is enabled.

You should be able to use {PATH_INFO} instead.

This bug report (against Drupal!) is the issue you're describing, I think: http://drupal.org/node/298016

There's a hotfix from Microsoft, but I haven't tried it: http://support.microsoft.com/kb/954946

like image 170
Dan Cosser Avatar answered Oct 19 '22 22:10

Dan Cosser


Short answer:

Use PATH_INFO server variable instead of REQUEST_URI, as you do not want to include the query string in the matching.

Full explanation:

This has caught me out before - basically it is a subtlety of using Rewrite Maps in the IIS URL Rewrite Module.

In your case, SiteMapEngine will be a static key-value list of URLs:

<rewrite>
    <rewriteMaps>
        <rewriteMap name="SiteMapEngine" defaultValue="">
            <add key="/page/abc/" value="/index.cfm?page=abc" />
            ...
        </rewriteMap>
    </rewriteMaps>
    ...
</rewrite>

The {SiteMapEngine:{REQUEST_URI}} condition in your rule checks whether there is a key in this rewrite map matching the REQUEST_URI server variable:

{REQUEST_URI} = /page/abc/?param1=111

Notice that this variable includes the query string - it therefore fails to find a matching key.

Instead, use the PATH_INFO server variable, which is the equivalent of REQUEST_URI but without the query string:

{PATH_INFO} = /page/abc/

So the correct rule is:

<rule name="Rewrite rule1 for SiteMapEngine">
    <match url=".*" />
    <conditions>
        <add input="{SiteMapEngine:{PATH_INFO}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="{C:1}" />
</rule>
like image 23
Dunc Avatar answered Oct 19 '22 22:10

Dunc


Here is my rule. It seems to work as expected:

<rule name="Insert index.cfm" enabled="true" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.cfm/{PATH_INFO}" appendQueryString="true" />
</rule> 
like image 43
Aaron Greenlee Avatar answered Oct 20 '22 00:10

Aaron Greenlee