Redirect your azurewebsites.net URL to your custom domain URL

If you’ve attached a custom domain to your Azure Websites setup, you probably want people and search engine crawlers to use the custom domain URL instead of the free azurewebsites.net URL. You can setup your application’s web.config to include the following.

Add the following to the system.webServer section of your web.config, but make sure you replace the “yourdomain” text with your own domain information.

  <rewrite>
    <rules>
    <rule name="Redirect old-domain to new-domain" stopprocessing="true">
      <match url=".*">
      <conditions>
      <add input="{HTTP_HOST}" pattern="^yourdomain.azurewebsites.net$">
      </add></conditions>
      <action type="Redirect" url="http://www.yourdomain.com/{R:0}" appendquerystring="true" redirecttype="Permanent">
    </action></match></rule>
    <rule name="WWW Rewrite" enabled="true">
      <match url="(.*)">
      <conditions>
      <add input="{HTTP_HOST}" negate="true" pattern="^www\.">
      <add input="{HTTP_HOST}" negate="true" pattern="localhost">
      </add></add></conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendquerystring="true" redirecttype="Permanent">
    </action></match></rule>
    </rules>
  </rewrite>