http to https rewrite too many redirect loops IIS 7

Put below input condition:

<add input="{HTTPS}" pattern="on" /> 

Instead of:

<add input="{HTTPS}" pattern="off" />

I also faced that problem. All requests to the server were HTTP. In my case problem was that I use Cloudflare DNS. There is SSL/TLS setting that by default SSL/TLS encryption mode is set to Flexible.

enter image description here

Make sure to change the mode to Full.


We have our ASP.NET application hosted on AWS with Elastic Load Balancing, and the rule in the question with the accepted answer did not work for us, and kept causing infinite redirects.

This is the rule that finally worked for us:

<rewrite>
   <rules>
      <rule name="HTTPS Rule behind AWS Elastic Load Balancer Rule" stopProcessing="true">
         <match url="^(.*)$" ignoreCase="false" />
         <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
         </conditions>
         <action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" />
      </rule>
   </rules>
</rewrite>

My case, I needed to put like this:

<rewrite>
<rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
        <add input="{HTTPS}" pattern="on" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"   redirectType="Found" />
    </rule>
</rules>