【问题标题】:Redirect to https and www not working重定向到 https 和 www 不起作用
【发布时间】:2018-07-16 18:01:01
【问题描述】:

我知道这个问题已经被问了 1000 次,但其他答案都没有帮助,所以我们来了。

我需要重定向:

http://example.comhttps://www.example.com

http://www.example.comhttps://www.example.com

这是我的规则:

<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="on" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>

<rule name="Redirect to WWW" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^example.com$" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>

结果:

http://example.com 重定向到https://www.example.com

http://www.example.com 重定向到 http://www.example.com(无 SSL)

我似乎无法让它正常工作。

任何帮助表示赞赏。谢谢!

【问题讨论】:

    标签: iis url-rewrite-module


    【解决方案1】:

    我认为问题在于您最初的 HTTP 到 HTTPS 规则。

    试试这个:

     <rule name="HTTP Redirect to HTTPS" patternSyntax="Wildcard" enabled="true" stopProcessing="true">
                        <match url="*" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTPS}" pattern="off" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
                    </rule>
    
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
          </conditions>
          <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
        </rule>
    

    【讨论】:

    • 谢谢@Calvin。我之前曾尝试过,但陷入了无休止的重定向循环。联系主机后,问题原来是负载均衡器正在终止 SSL 请求,这导致了无限的重定向循环。不过我很感激这个答案。
    • 没问题。我认为无限循环是由您使用 pattern="on" 而不是 off 引起的。不过,很高兴它解决了。
    • 我知道,对吧?这让我发疯了。每次我将它设置为off 时,它都会进入无限循环。这就是为什么我拥有它on
    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2017-01-11
    • 2020-02-03
    • 2016-05-24
    • 2018-10-24
    • 2020-05-22
    相关资源
    最近更新 更多