Inserting Rewrite rule in Release Config

Adding rewrite rule in release config is handy so that it will not affect during Debug mode.

commit 14e75de · authored · 2 min read

Inserting Rewrite rule in Release Config

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

<system.webServer>
....
<!--If you wish to use IIS rewrite rules,
see the documentation here:https://our.umbraco.org/documentation/Reference/Routing/IISRewriteRules-->
<!--<rewrite>
<rules></rules>
</rewrite> -->
</system.webServer>

And my Web.Release.config looked like this,

<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</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,

<system.webServer>
....
<!--If you wish to use IIS rewrite rules,
see the documentation here:https://our.umbraco.org/documentation/Reference/Routing/IISRewriteRules-->
<rewrite>
<rules></rules>
</rewrite>
</system.webServer>

And my Web.Release.Config to,

<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm" />
</customErrors>
-->
</system.web>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

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

<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

local graph · open full graph

"Inserting Rewrite rule in Release Config" is connected to the topics Debug, Web.config, Rewrite Rules and related to 6 other entries. Redirect HTTPS requests to HTTP using IIS Rewrite rule in Web.Config Redirect HTTPS requests to … Redirect HTTP to HTTPS using Web.Config in IIS Redirect HTTP to HTTPS usin… Redirect WWW to Non-WWW using Web.Config in IIS Redirect WWW to Non-WWW usi… Redirect non-www urls to www urls Redirect non-www urls to ww… C# - Get Web.Config Connection String in a Class Library C# - Get Web.Config Connect… IIS - Disable CORS IIS - Disable CORS Debug #debug Web.config #web-config Rewrite Rules #rewrite-rule Inserting Rewrite rule in Release…

webmentions · 1 mention

comments

loading discussion…