Question

I have an asp.net application, which must run under SSL, and it has some rewrite rules defined in web.config to accomplish this.

<!--file web.config -->
....
</system.webServer>
  <rewrite>
    <rules configSource="webrewrite.config" />
  </rewrite>
</system.webServer>


<!--file web.config -->
<rules>
  ....    
  <rule name="HTTP to HTTPS redirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
  </rule>
  ....
</rules>

However, in development mode (with local web server or IIS Express) I don't want to use SSL. So I would like to be able to use web.config transformations to remove one or more rewrite rules (but not all)

Was it helpful?

Solution 2

I solved the problem, by using Remove transform, as shown below

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  ....
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RulaNameToRemove"
          xdt:Transform="Remove"
          xdt:Locator="Match(name)" >
        </rule>
      </rules>
    </rewrite>    
  </system.webServer>
</configuration>

OTHER TIPS

If you want to remove the Entire Section for your Dev Configuration use

<system.webServer>
   <rewrite xdt:Transform="Remove" >
   </rewrite>
 </system.webServer>

Please write below code web.Debug and web.Release config file. Web.Debug will delete rewrite rules and web.Release insert rewrite rules.

Web.Debug.config

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RulaNameToRemove"
          xdt:Transform="Remove"
          xdt:Locator="Match(name)" >
        </rule>
    </rewrite>    
  </system.webServer>
</configuration>

Web.Release.config

<system.webServer>
    <rewrite>
      <rules>
        <clear />
        <rule name="Redirect to https" stopProcessing="true" xdt:Transform="Insert">
          <match url=".*"/>
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="https://{SERVER_NAME}" redirectType="Permanent"/>
         
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

You can move release settings to web.config.release file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top