debug | web-config | rewrite-rule |

Inserting Rewrite rule in Release Config

by Abhith RajanAugust 12, 2017 · 3 min read · Last Updated:
Share this

My Web.config’s system.webServer section initially looked like this,

1<system.webServer>
2....
3<!--If you wish to use IIS rewrite rules,
4see the documentation here:https://our.umbraco.org/documentation/Reference/Routing/IISRewriteRules-->
5 <!--<rewrite>
6 <rules></rules>
7 </rewrite> -->
8</system.webServer>

And my Web.Release.config looked like this,

1<?xml version="1.0" encoding="utf-8"?>
2
3<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
4
5<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
6 <!--
7 In the example below, the "SetAttributes" transform will change the value of
8 "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
9 finds an attribute "name" that has a value of "MyDB".
10
11 <connectionStrings>
12 <add name="MyDB"
13 connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
14 xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
15 </connectionStrings>
16 -->
17 <system.web>
18 <compilation xdt:Transform="RemoveAttributes(debug)" />
19 <!--
20 In the example below, the "Replace" transform will replace the entire
21 <customErrors> section of your web.config file.
22 Note that because there is only one customErrors section under the
23 <system.web> node, there is no need to use the "xdt:Locator" attribute.
24
25 <customErrors defaultRedirect="GenericError.htm"
26 mode="RemoteOnly" xdt:Transform="Replace">
27 <error statusCode="500" redirect="InternalError.htm"/>
28 </customErrors>
29 -->
30 </system.web>
31</configuration>

Now I have to add a rule which redirects all non-www requests to www route. I accomplished the same by modifying the Web.config to,

1<system.webServer>
2....
3<!--If you wish to use IIS rewrite rules,
4see the documentation here:https://our.umbraco.org/documentation/Reference/Routing/IISRewriteRules-->
5 <rewrite>
6 <rules></rules>
7 </rewrite>
8</system.webServer>

And my Web.Release.Config to,

1<?xml version="1.0" encoding="utf-8"?>
2
3<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
4
5<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
6 <!--
7 In the example below, the "SetAttributes" transform will change the value of
8 "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
9 finds an attribute "name" that has a value of "MyDB".
10
11 <connectionStrings>
12 <add name="MyDB"
13 connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
14 xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
15 </connectionStrings>
16 -->
17 <system.web>
18 <compilation xdt:Transform="RemoveAttributes(debug)" />
19 <!--
20 In the example below, the "Replace" transform will replace the entire
21 <customErrors> section of your web.config file.
22 Note that because there is only one customErrors section under the
23 <system.web> node, there is no need to use the "xdt:Locator" attribute.
24
25 <customErrors defaultRedirect="GenericError.htm"
26 mode="RemoteOnly" xdt:Transform="Replace">
27 <error statusCode="500" redirect="InternalError.htm" />
28 </customErrors>
29 -->
30 </system.web>
31 <system.webServer>
32 <rewrite>
33 <rules>
34 <clear />
35 <rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">
36 <match url=".*" />
37 <conditions>
38 <add input="{HTTP_HOST}" pattern="^(?!www)(.*)$" />
39 </conditions>
40 <action type="Redirect" redirectType="Permanent" url="https://www.{C:1}/{R:0}" />
41 </rule>
42 </rules>
43 </rewrite>
44 </system.webServer>
45</configuration>

Noticed the xdt:Transform=“Insert” in the line,

1<rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">

Which will add the rule to the Web.config automatically during publishing.

This article is part of my daily google series. And this is the very first post in this category.

Cover photo by Caspar Rubin

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Abhith Rajan

Written by Abhith Rajan Buy me a coffee
Abhith Rajan is an aspiring software engineer with more than 6 years of experience and proven successful track record of delivering technology-based products and services.

Related Posts