Ensure HTTPS and www

Ensure HTTPS and www

When sites are in production you'll want it to use HTTPS. To redirect HTTP traffic you can use rewrite rules in the web.config. If the site is public facing and therefore SEO is important you'll also want one url to always be used. The following is one way to achieve this by modifying the web.config.

Ensure HTTPS

<rule name="Redirect Rule for PermanentRedirects" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{PermanentRedirects:https://{HTTP_HOST}{REQUEST_URI}}" pattern="(.+)" />
  </conditions>
  <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
</rule>

Ensure www

<rule name="ensurewww" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>