Update URL rewrite rules with vs2012 web deployment Convert web.config

I cannot figure out how to make my web.config deployment transform work for the rewrite rule. I tried the following and it ignores it.

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">


 <system.webServer>
 <rewrite xdt:Transform="Replace">
  <rules>

    <rule name="Force HTTPS On Login/Register" stopProcessing="true">
      <match url="Account/Login(.*)|Register(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
    </rule>


    <rule name="Force HTTPS Off" stopProcessing="true">
      <match url="((Account/Login(.*))|(Register(.*)))" negate="true" ignoreCase="true" />
      <conditions>
        <add input="{HTTPS}" pattern="^ON$" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>


  </rules>
 </rewrite>
</system.webServer>
+5
source share
1 answer

I use SlowCheetah to convert my web.config to production. I initially tried what you tried, but found that I had to add an empty

<rewrite>
  <rules />
</rewrite>

to web.config database

and then write a transformation like

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

(this is a forwarding conversion, but I think the same principle should apply).

Note xdt:Transform="Insert"to insert a new node into the skeletal <rules />in the base configuration file.

+6

All Articles