【问题标题】:IIS: Remove http and force httpsIIS:删除 http 并强制 https
【发布时间】:2020-01-17 20:16:57
【问题描述】:

我有一个简单的网站,我想去掉 www 并强制它是 https。

我设法让 3 种可能的组合起作用,但被最后一种卡住了。


我正在使用从此处提出的其他问题借用的代码进行 URL 重写。我启用了以下 2 条规则:

    <rule name="Remove www" enabled="true" stopProcessing="true">
      <match url="(.*)" negate="false"></match>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Force HTTPS" enabled="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>

我有以下 URL 重定向:

知道如何应对这种情况吗?

谢谢

【问题讨论】:

  • 根据你的url重写规则,我自己做了一个测试demo,效果很好。 Result Image。我建议您可以尝试使用失败请求跟踪来解决发生的问题。详情可以参考这个article

标签: redirect iis url-rewriting


【解决方案1】:

您是否尝试更改规则的顺序?有时这可能会有所帮助。

【讨论】:

    【解决方案2】:

    感谢您的意见。

    这就是我解决问题的方法。

    <rule name="Remove www" enabled="true">
      <match url="(.*)" negate="false"></match>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    
    <rule name="Force HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    我在第二条规则中添加了stopProcessing标签only

    我修改了我的 DNS 记录,只留下以下规则:

    CNAME Record    www     website.tld.
    

    在 IIS 中,我添加(恢复)了带有和不带有 www 的域的绑定。

    我对为什么会这样的原因如下

    • 该网站可以同时处理 www.website.tld 和 website.tld。
    • URL 重写规则将处理重定向。

    对该站点的调用可能会删除 www 以及转移到 https。允许重写规则同时执行这两种操作(禁用 stopProcessing)允许这种情况发生。

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 2021-06-05
      • 1970-01-01
      • 2018-09-14
      • 2019-05-01
      相关资源
      最近更新 更多