Skip to content

Instantly share code, notes, and snippets.

@alindgren
Created December 2, 2016 17:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alindgren/999c542a6625ddfb41f1b0293878dcad to your computer and use it in GitHub Desktop.
Save alindgren/999c542a6625ddfb41f1b0293878dcad to your computer and use it in GitHub Desktop.
Canonical Domain Redirect for Azure App Service deployment slots

I often create a canonical host name URL Rewrite rule for a production site so that requests redirect to a single domain -- for example from alexlindgren.com to www.alexlindgren.com. For sites hosted with Azure App Service using deployment slots, we only want the production slot to redirect, otherwise going to the staging slot will redirect you to production since each slot uses uses the same web.config. To handle this, one can just add the non production domains to the conditions as seen in this gist.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true"/>
<add input="{HTTP_HOST}" pattern="^sitename-staging\.azurewebsites\.net$" negate="true"/>
<add input="{HTTP_HOST}" pattern="^www\.sitename\.com$" negate="true"/>
</conditions>
<action type="Redirect" url="http://www.sitename.com/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment