Search code examples
wcfiisurl-rewritingiis-7.5

IIS Rewrite very confusing


concerning IIS Rewrites.

I want to change this url on my local IIS Instance

http://localhost/MySite/health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c

to redirect to this url:

http://localhost/MySite/Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c

As you can see I don't want the incoming request to use

Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c

instead to use

health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c

The Health.svc is the WCF endpoint name, so I just want /health with the key and filter parameter at the end as shown.

Whatever I put in my web config rewrite it still doesn't work. I am rather confused what bit of the url to put in, as the regex seems to be valid as I can test it in IIS and online regex validators.

<rewrite>
  <rules>
    <rule name="HealthRewrite" stopProcessing="true" enabled="true">
      <match url="MySite\/health\?key=([0-9a-zA-Z=]+)&amp;([a-z])" />
      <action type="Rewrite" url="MySite/Health.svc/{R:1}/{R:2}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

How can I get this to work? I have got the rewrite module installed as can see it in IIS an also can see the dll is registered.


Solution

  • I managed to get it working with going to all sorts of sites as its not obvious at all.

    They key seemed to put the conditions in and then a {QUERY_STRING} with regex which can then create the {C:1} and {C:2} groups that are pushed into the new rewrite

    <rewrite>
      <rules>
        <rule name="HealthRewrite" stopProcessing="true" enabled="true">
          <match url="^health" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{QUERY_STRING}" pattern="key=([0-9a-zA-Z=]+)&amp;([a-z])" />
          </conditions>
          <action type="Rewrite" url="/MySite/Health.svc/{C:1}/{C:2}" appendQueryString="false"/>
        </rule>
      </rules>
    </rewrite>
    

    I found it confusing knowing what url to match but with a few simplified tests of just ^health I could see more easily and play around with getting the query string parameters. I had to provide the Rewrite with the /MySite/ prefix which is confusing as the match didn't need that!