简体   繁体   中英

IIS URL Rewrite https rule ignoring localhost

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 (eg 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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM