Inserting Rewrite rule in release config

3.5k Views Asked by At

Hi i want to insert a rewrite rule for "Redirect to HTTPS" but only on my release config

This is how the rewrite rule looks

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Redirect to HTTPS">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    <add input="{URL}" pattern="/$" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

How to achiev this only in my release.config?

2

There are 2 best solutions below

0
On

You can look at web.config transformations: https://msdn.microsoft.com/library/dd465318(v=vs.100).aspx

To create and code a transform file

  1. If no transform file exists for the build configuration that you want to specify settings for, in Solution Explorer, right-click the Web.config file and then click Add Config Transforms
  2. Open the transform file for the build configuration that you want to work with.
  3. Edit the transform file to specify the changes that should be made to the deployed Web.config file when you deploy by using that build configuration. The default transform file includes comments that show how to code some common transforms.
0
On

Simply add xdt:Transform="Insert" attribute on the element that you need to insert to your release version of the web.config. For example, if your initial web.config doesn't contain <rewrite> element at all, then the release.config should be as follow :

<system.webServer>
    <rewrite xdt:Transform="Insert">
      <rules>
        <rule name="Redirect to HTTPS">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{URL}" pattern="/$" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>

Otherwise, if the initial web.config already contains some other rules, then you only need to add the xdt:Transform="Insert" attribute at <rule> element level :

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS" xdt:Transform="Insert">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{URL}" pattern="/$" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>