Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing IIS rewrite rules in web.config transform

I have some IIS rewrite rules that I want to vary by environment. The development rewrite rules are in the web.config file, then at the end of the web.test.config file I have:

    <appSettings>          ...Some app settings tranforms here     </appSettings>     <system.webserver>             <rewrite xdt:Transform="Replace">               <rules>                 ... rules here               </rules>             </rewrite>           </system.webserver>         </configuration> 

My app settings are getting transformed when I deploy to test, but by IIS rewrite rules are not. I was hoping the entire <rewrite> section would simply be replaced with the one in the transform file (as per http://msdn.microsoft.com/en-us/library/dd465326.aspx), but nothing is changing.

I have tried putting xdt:Transform="Replace" xdt:Locator="Match(name)"> on the individual rules too:

<rule name="Test rule" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)"> 

But again this makes no difference.

Is it even possible to replace rewrite rules in the web.config and if so, what am I missing?

like image 988
LDJ Avatar asked Aug 08 '12 07:08

LDJ


People also ask

Where are IIS rewrite rules stored?

When done on the server level it is saved in the ApplicationHost. config file. You can also define it on the folder level, it that case it is saved in a web. config file inside that folder.

How do I disable URL Rewrite in IIS?

Open Internet Information Services (IIS) Manager > Servername > Sites > example.com > URL rewrite. Select each rewrite rule and disable them by clicking on Disable Rule.


2 Answers

As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work. I successfully used the Insert transform, as below:

  <system.webServer> <rewrite xdt:Transform="Insert">   <rules>     <rule name="CanonicalHostNameRule1">       <match url="(.*)" />       <conditions>         <add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" negate="true" />       </conditions>       <action type="Redirect" url="http://www.mysite.com/{R:1}" />     </rule>   </rules> </rewrite> </system.webServer> 
like image 161
StuartQ Avatar answered Sep 18 '22 21:09

StuartQ


There is a lot of answers here with examples which is a good thing, but I think few details are missing. I have wrote about this in my website, the key point here is to add xdt:Transform="Insert" in the root tag hierarchy you want to be added for the respective environment.

By default you have your Web.config file, but you have also Web.Debug.config and Web.Release.config as seen in the image below:

enter image description here

Lets say you want to added a redirection from http to https in your release of the application. Then edit Web.Release.config and add following lines:

<?xml version="1.0"?>  .....   <system.webServer>     <rewrite xdt:Transform="Insert">       <rules>         ......       </rules>     </rewrite>   </system.webServer> </configuration> 

So next time you publish your project the tag with rewrite and its sub-content will be added to web.config file.

To see that before you publish, right click on Web.Release.config and click Preview Transform.

enter image description here

You will see the difference between initial version and release version.

enter image description here

Reference:

  • HTTP to HTTPS Redirect - IIS 8.5 not working properly
  • Microsoft Web.Config file transformations

Disclaimer: the link of this guideline refer to my personal web site.

like image 28
Maytham Avatar answered Sep 19 '22 21:09

Maytham