Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating url rewriting rule with vs2012 web deployment web.config transformations

I can't figure out how to get my web.config deployment transformation to work for a rewrite rule. I've 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>
like image 534
Peter Kellner Avatar asked May 02 '13 04:05

Peter Kellner


People also ask

How do I create a new transformation in web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

What is Xdt transform in web config?

The xdt:Transform="RemoveAttributes(debug)" attribute specifies that you want the debug attribute to be removed from the system. web/compilation element in the deployed Web. config file.

How does IIS URL Rewrite work?

The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.


1 Answers

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

<rewrite>
  <rules />
</rewrite>

to the base web.config

and then write a transform 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>

(that is a redirect transform, but I think the same principal should apply).

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

like image 155
Eric J. Avatar answered Oct 07 '22 00:10

Eric J.