Using Url Rewrite to Redirect Naked and Azure Website Domains

When I put up a new website in Azure I tend to run into the same problems. Pointing DNS to the azure website nets a lot of urls. I'll have mydomain.com, www.mydomain.com, mydomain.azurewebsites.net, and http/https versions of all of them. When talking SEO, you generally don't want duplicate ways to get to the same content. Luckily, this can be solved relatively painlessly with url rewrites in the web.config. I put together a quick reference to handle some of the scenarios.

General

Inside the web.config of an Asp.Net site rewrite rules are added via the rewrite module. These examples are simple redirections, but full reverse proxies can be configured this way. Here is a simple structure:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule1">
                    <!-- rule information -->
                </rule>
                <rule name="rule2">
                    <!-- rule information -->
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

After pointing mywebsite.com (via ip address) and www.mywebsite.com (via hostname) to the Azure App Service, redirections get added to make sure the user only accesses the website from one place.

Redirecting naked domains to www

If both mywebsite.com and www.mywebsite.com get pointed to the same place they can be consolidated with a simple redirect rule.

<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="yourwebsite.com" />
    </conditions>
    <action type="Redirect" url="https://www.yourwebsite.com/{R:0}" />
</rule>

Redirecting azurewebsites.net domains to www

Azure is nice enough to expose a public url for whatever Azure App Service gets created. Unfortunately, once DNS gets configured, there are now two different ways to get to the same place. This can be solved with another redirect rule.

<rule name="Redirect azure to domain" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="yourwebsite.azurewebsites.net" />
    </conditions>
    <action type="Redirect" url="https://www.yourwebsite.com/{R:0}" />
</rule>

Redirecting http to https

I would no longer recommend doing this with the web.config. Azure has a setting that allows you to automatically redirect http to https.

  • Go to the azure app service
  • Click DNS settings
  • Flip on the toggle switch for Https always on

Easy peasy. But, for those who don't have access to this setting I'll provide a solution anyways.

<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
    <match url="(.*)"/>
    <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://www.yourwebsite.com/{R:1}" />
</rule>

Those are the common scenarios that I tend to run into. I'll post future articles on setting up a full reverse proxy with Azure.

For more information you can refer to:

https://social.msdn.microsoft.com/Forums/en-US/101c4e3d-bb2f-4a76-b39a-cfc5a95c7ee7/azure-naked-domain-redirection-to-www?forum=windowsazurewebsitespreview