Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Please help me understand rewrite maps, I have this setup:

<rewriteMap name="MyMap">
   <add key="/pages" value="/website/pages/index.aspx" />
   <add key="/pages/page-1" value="/website/pages/page-1/index.aspx" />
   <add key="/pages/page-1/section-1" value="/website/pages/page-1/section-1.aspx" />
</rewriteMap>

Here is my rule:

<rule name="Rewrite rule for MyMap" stopProcessing="true">
   <match url=".*" />
   <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{MyMap:{REQUEST_URI}}" pattern="(.+)" />
   </conditions>
   <action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>

This is working if I try to access these pages:

  • www.mydomain.com/pages (OK)
  • www.mydomain.com/pages/page-1 (OK)
  • www.mydomain.com/pages/page-1/section-1 (OK)

But I want the possibility to add a query string on any of these pages, example:

  • www.mydomain.com/pages?page=1 (FAIL)

When I try to access this page, I get a 404. How can I get this to work?

share|improve this question

migrated from stackoverflow.com Oct 24 '11 at 18:51

This question came from our site for professional and enthusiast programmers.

2 Answers 2

By default URL rewrite rules using rewrite maps are created matching on the {REQUEST_URI} variable. This includes the URL + the query string. This allows you to match against URLs with query string parameters and rewrite them to other pages. Very handy but this is apparently not what you want/need.

You want to only match against the URL, without the query string. In order to change this you have to replace {REQUEST_URI} in the condition to {URL}. Then only the URL (without query string) will be matched against the rewrite map. You can then add the query string to the rewritten URL so that the executed script still has access to it.

You end up with a rule like:

<rule name="Rewrite rule for MyMap">
    <match url=".*" />
    <conditions>
        <add input="{MyMap:{URL}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
share|improve this answer
    
Great explanation. I'd been wondering how to ignore the QueryString for match purposes, too. –  dthrasher Jan 30 '13 at 19:01

Instead of using

<add input="{MyMap:{URL}}" pattern="(.+)" />

you can also try

<add input="{MyMap:{SCRIPT_NAME}}" pattern="(.+)" />

See here:

Can't get static redirects to work with IIS Url Rewrite 2.0

share|improve this answer
    
Thanks, I will look into this. –  Martin Nov 16 '11 at 18:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.