Replacing IIS rewrite rules in web.config transform

LDJ picture LDJ · Aug 8, 2012 · Viewed 33.7k times · Source

I have some IIS rewrite rules that I want to vary by environment. The development rewrite rules are in the web.config file, then at the end of the web.test.config file I have:

    <appSettings>
         ...Some app settings tranforms here
    </appSettings>
    <system.webserver>
            <rewrite xdt:Transform="Replace">
              <rules>
                ... rules here
              </rules>
            </rewrite>
          </system.webserver>
        </configuration>

My app settings are getting transformed when I deploy to test, but by IIS rewrite rules are not. I was hoping the entire <rewrite> section would simply be replaced with the one in the transform file (as per http://msdn.microsoft.com/en-us/library/dd465326.aspx), but nothing is changing.

I have tried putting xdt:Transform="Replace" xdt:Locator="Match(name)"> on the individual rules too:

<rule name="Test rule" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">

But again this makes no difference.

Is it even possible to replace rewrite rules in the web.config and if so, what am I missing?

Answer

StuartQ picture StuartQ · Nov 27, 2013

As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work. I successfully used the Insert transform, as below:

  <system.webServer>
<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="http://www.mysite.com/{R:1}" />
    </rule>
  </rules>
</rewrite>
</system.webServer>