IIS: Redirect HTTP to HTTPS

Published:

To cleanly redirect HTTP to HTTPS with IIS, first install the URL Rewrite module, and then add the following to your web.config. Also remember to have bindings defined for your site for both HTTPS(443) and HTTP(80).

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action
            type="Redirect"
            redirectType="Permanent"
            url="https://{HTTP_HOST}/{R:1}"
          />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>