首页 > 解决方案 > 将规则重写为 HTTPS 和 WWW,除非在本地主机上

问题描述

由于我的粗心,删除了以前的帖子并发布了实时域。

我在构建一个正常运行的 Web.config 规则时遇到了麻烦,该规则将引导来自http:// to https://wwwand的流量http://www to https://www,除了在我进行某些开发https:// to https://wwwlocalhost或在进行某些开发时。127.0.0.1

网页配置

<rewrite>
  <rules>
    <clear />
    <rule name="Redirect non-www OR non-https to https://www">
      <match url=".*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^example.com$" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
    </rule>
  </rules>

更新和工作规则,也许有人可以指出一些错误?

<rewrite>
  <rules>
    <clear />
    <rule name="Enforce HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" matchType="Pattern"
              pattern="^localhost(:\d+)?$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
            appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Redirect example.com to www.example.com"
           enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^[^www]" />
        <add input="{HTTP_HOST}" matchType="Pattern"
              pattern="^localhost(:\d+)?$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:1}"
           appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

标签: asp.netiisurl-rewritingweb-config

解决方案


推荐阅读