H is for Harre.

harre.dev

Tags: Hosting IIS Azure

Web.config domain rewriting

Here’s a quick and easy domain rewriting solution running on an Azure App service.

Some assumptions before you can start though:

To make things even easier, you can set this whole thing up from inside the Azure portal using the Advanced Tools option which opens the Kudu interface for the web server. In the Kudu interface click on Debug Console -> CMD. Next, browse to the wwwroot directory at site/wwwroot. From there you can create new files.

Create a web.config file in the wwwroot folder. Set the contents of the web.config file to the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.webServer>
        <rewrite>
          <rules>
            <rule name="redirect" enabled="true">
              <match url="(.*)" />
              <action type="Redirect" url="https://example.net/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>
   </system.webServer>
</configuration>

It will match any url (using the regex (.*)) and set a permanent redirect to https://example.net. The {R:1} backreference will preserve any path that was sent with the original url.

Word of caution though, every change to web.config results in the web server restarting.

Resources