IIS URL Rewrite https rule ignoring localhost

One of many picture One of many · Oct 6, 2014 · Viewed 12.8k times · Source

I'm trying to write a URL rewrite rule to force a HTTPS connection. This should always happen except when a request is using localhost (e.g. http://localhost/mysite).

The rule is configured as following:

 <rule name="Redirect to https" enabled="true" stopProcessing="true">
      <match url="(.*)" negate="false" />
      <conditions trackAllCaptures="false">
           <add input="{HTTPS}" pattern="^OFF$" />
           <add input="{URL}" pattern="localhost" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
 </rule>

I also tried to use ^localhost and ^localhost/(.*) as a pattern for the URL condition with no help. Does anyone have an idea why this does not work and what a solution for this problem should be?

Answer

Justin Iurman picture Justin Iurman · Oct 6, 2014

Your code should look like this instead

<rule name="Redirect to https" enabled="true" stopProcessing="true">
   <match url="(.*)" />
   <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTPS}" pattern="off" />
      <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
   </conditions>
   <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>