【问题标题】:IIS Redirect HTTP to HTTPS and WWW to non-WWWIIS 将 HTTP 重定向到 HTTPS,将 WWW 重定向到非 WWW
【发布时间】:2019-12-13 10:43:43
【问题描述】:

在带有 URL 重写模块 2.0 的 IIS 10 上,我需要 2 条规则

1) HTTP 转 HTTPS

2) WWW 到非 WWW

第一个由空白规则模板创建。 对于第二个,我使用规范域名模板。

在我的 web.config 规则中是这样的:

<rewrite>
  <rules>
    <rule name="ForceHttps" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
    <rule name="CanonicalHostNameRule1" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://cooltechunder.com/{R:1}" />
    </rule>
  </rules>
</rewrite>

所有案例都可以正常工作,除非有一个以:http://www 开头。

查看此图片以了解我的结果: https://i.imgur.com/h2l3Yw6.png

【问题讨论】:

    标签: asp.net-core-2.2 url-rewrite-module iis-10


    【解决方案1】:

    你写了stopProcessing="true"

    这意味着,如果规则匹配,后续规则将被跳过。

    来自the documentation

    规则可能会打开 StopProcessing 标志。当规则动作被执行(即规则匹配)并且该标志被打开时,意味着将不再处理后续规则,并且请求将被传递到IIS请求管道。默认情况下,此标志是关闭的。

    在我看来,这是你所描述的你不想要的情况。

    所以,删除它。

    【讨论】:

    • 我删除了 StopProcessing 标志但仍然得到相同的结果((
    • 修改配置后需要采取什么步骤吗?例如重新启动服务或重新加载配置?应该是这样的。
    • 我重新启动了 IIS,但结果相同(
    【解决方案2】:

    好的,我的问题与绑定有关。

    我必须在绑定中指定“主机名”,因为其他网站也使用特定端口

    我也忘了添加“www”版本的绑定。

    现在我的绑定看起来像这样:https://i.imgur.com/Lhdv4nS.jpg

    我还把重写代码改成了更紧凑的代码:

    <rewrite>
      <rules>
        <rule name="Https and non-www">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="https://cooltechunder.com/{R:1}" />
        </rule>
      </rules>
    </rewrite>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 2012-04-14
      • 1970-01-01
      • 2018-07-25
      相关资源
      最近更新 更多