How do I make URL rewrite work with web.Release.config transform?
Asked Answered
C

4

46

I have a web.config rewrite rule specified to move all traffic to https. The rule works, but I don't want SSL required while I am debugging. I have a bunch of web.release.config transformations being done already that work on publish so I decided to put a rewrite rule in there. The problem is that the rewrite rule isn't being transformed like the rest of the settings. Here is the web.config setup:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>

    <rewrite></rewrite>
</system.webServer>

And here is the transformation being done:

  <system.webServer>
<rewrite>
  <rules>
    <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
      <match url="(.*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$"/>
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
    </rule>
  </rules>
</rewrite></system.webServer>

If I just copy the rewrite rule to the web.config it works fine. Does anyone out there have any ideas why web.Release.config transforms aren't working for only this section?

Casebook answered 9/8, 2011 at 23:0 Comment(1)
I would also love to know what's up with this. The transform doesn't seem to behave like it should.Sherbrooke
A
45

Transformation will only happen if you put proper xdt attributes on the elements that need to be transformed. Try adding an xdt:Transform attribute to your release config:

<system.webServer xdt:Transform="Replace">
    <!-- the rest of your element goes here -->
</system.webServer>

That will tell the transformation engine that the entire system.webServer element from Web.config needs to be replaced with the one from Web.Release.config.

The transformation engine will silently ignore any elements that do not have xdt attributes.

Obligatory link to MSDN.

Alkali answered 10/8, 2011 at 22:58 Comment(7)
Hey, thanks a ton. I saw some examples that didn't explicitly put the transform. This is a huge lifesaver. I should check the documentation better in the future, but I appreciate it.Casebook
I'd +4654654564324 if I could. I've wondered for a while why I could never get this to work. It'd be nice if the transformation engine would spit out a warning when ignoring an element.Mvd
Extra note for people with more complicated files. You can put the xdt:Transform element on the <rewrite> tag and change it to Insert like this: <rewrite xdt:Transform="Insert"> which will preserve any other tags in your <system.webServer> section.Eellike
I hate web.config transformations. Who designs this.Plasticity
Actually web.config transformations are very slick... some people are just stuck in the past...Vashti
Whats the good schema to use? I have 'schemas.microsoft.com/XML-Document-Transform' and VS 2015 tells me that xdt:Transform is not declared on <rule>Yarn
@Stephane looks like the schema likes the Transform in parent <system.webServer> but does not expect it in <rewrite> or <rules> underneath. But others report it works anyway.Romaic
M
33

Another way to go would be to put in a rewrite condition that negates if you are on localhost:

<conditions>
    <add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>
Manvel answered 21/8, 2011 at 7:41 Comment(1)
Awesome solution. Worth noting if you're using Chrome - Chrome caches the redirect - so you may need to clear the cache: superuser.com/questions/304589/…Rheo
D
10
<system.webServer>
    <rewrite>
        <rules xdt:Transform="Replace">
            <clear />
            <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
              <match url="(.*)" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern="localhost(:\d+)?" negate="true" />
                <add input="{HTTP_HOST}" pattern="127\.0\.0\.1(:\d+)?" negate="true" />
                <add input="{HTTPS}" pattern="OFF" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
            </rule>
        </rules>          
    </rewrite>
</system.webServer>
Denounce answered 22/6, 2015 at 19:27 Comment(1)
you can also use <rules xdt:Transform="Insert"> if your web.config doesn't have the rewrite elements in it.Denounce
J
2

Summing up other answers here, we discovered the obvious: "Replace" will only replace a node, not "Insert" it (thanks DigitalD for the right track). The rest of our transformation files use replace so we opted for an empty tag in our base web.config (the one that gets transformed).

<system.webServer>
...other tags here that do not get transformed...
<rewrite />
</system.webServer>

Ideally there would be "Overwrite" which would Insert or Replace (or Remove and Insert).

Jehias answered 10/9, 2013 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.