【问题标题】:IIS rewrite rule to redirect https to http not workingIIS 重写规则以将 https 重定向到 http 不起作用
【发布时间】:2015-08-10 04:55:43
【问题描述】:

这里有很多关于将 http 重定向到 https 的问题,所以我认为逆转这个过程很容易。但是,我尝试过的一切都没有奏效。

我正在尝试将该规则与我的规范主机名规则结合起来(这是第一条规则,位于重写规则的顶部):

<rule name="CanonicalHostName" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
    <add input="{HTTPS}" pattern="^ON$" />
    <add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com|example-staging\.azurewebsites\.net$" />
  </conditions>
  <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

该站点托管在 Azure 上,DNS 使用 CloudFlare,如果这有什么不同的话,我相信它不应该。

任何想法我做错了什么/可能会阻止规则的 https 到 http 部分工作? (主机名部分工作正常)

【问题讨论】:

  • 应用是MVC吗?您是否在全局过滤器或控制器本身中有 RequireHttpsAttribute
  • 是的,这是一个 MVC Web 应用程序,但在任何地方都没有 RequireHttpsAttribute。

标签: asp.net iis url-rewriting web-config


【解决方案1】:

您的重定向仍然是 http

url="http://www.example.com/{R:1}"

您可以按照以下说明操作:Click Here

补充资料:Here

【讨论】:

  • 没错——我想强制使用 http,而不是 https。还是我错过了什么?
【解决方案2】:

CloudFlare

您无法从 SSL 重定向的原因似乎是因为您使用的是 CloudFlare。 CloudFlare 至少使用灵活的 SSL。这意味着最终用户,浏览器显示 SSL 锁定,但您的服务器不需要 SSL。请参阅此处的文档:https://www.cloudflare.com/ssl

如果没有 CloudFlare,以下示例应该可以工作。

没有 CloudFlare

以下规则应该有效。如果你愿意,你仍然可以添加你的否定。

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

我的工作演示站点的完整重写部分。

<rewrite>
    <rules>
        <rule name="CanonicalHostNameRule1">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.ashleymedway\.com$" negate="true" />
            </conditions>
            <action type="Redirect" url="http://www.ashleymedway.com/{R:1}" />
        </rule>
        <rule name="HTTPS to HTTP redirect" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="on" />
            </conditions>
            <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" redirectType="Found" />
        </rule>
    </rules>
</rewrite>

【讨论】:

  • @Gavin Odd,您的设置一定有问题,我已将我的回答中的规则添加到ashleymedway.com,您可以看到该规则在这里有效。
  • 感谢您提供完整示例并再次尝试。我现在的规则和你的基本一致。还是同样的问题。我的网站是 www.topomap.co.nz - 如果您尝试 preview.topomap.co.nz 则规范规则有效,但 https 永远不会。我想知道它是否可能与 Azure 或 CloudFlare 有关?很奇怪!
  • @Gavin Cloudflare 需要 https,请参阅此处cloudflare.com/ssl。请参阅灵活 SSL 部分
  • 感谢您,刚刚关闭 Crypto - SSL (with SPDY) 设置,重定向现在按预期工作。
  • @Gavin 太棒了,很高兴我们把它整理好了 :)
猜你喜欢
  • 2013-06-02
  • 2018-07-30
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2019-10-05
  • 1970-01-01
相关资源
最近更新 更多