Redirect to HTTP behind load balancer (apart from localhost) with URL rewrite

Datetime:2016-08-22 22:57:26          Topic:          Share

Just a short "note to self for future" post as found a few variations for part of this online but needed to pull them together.

Requirement is:

  • Ensure all HTTP requests are redirected to HTTPS
  • Handle the fact that the web server is behind a load balancer that terminates HTTPS
  • Allow for local HTTP requests via a custom port
<rewrite>
      <rules>
        <rule name="Redirect to https" enabled="true" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
            <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
          </conditions>
         <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>