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?
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.
Open Internet Information Services (IIS) Manager > Servername > Sites > example.com > URL rewrite. Select each rewrite rule and disable them by clicking on Disable Rule.
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>
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:
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.
You will see the difference between initial version and release version.
Reference:
Disclaimer: the link of this guideline refer to my personal web site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With