【问题标题】:ASP.NET - rewriting all https requests to httpASP.NET - 将所有 https 请求重写为 http
【发布时间】:2013-12-03 07:06:40
【问题描述】:

我的问题正是here 提出的问题,我决定尝试将所有https 请求重写为http。我已经进行了漫长而艰苦的搜索,但似乎没有明确的方法来实现这一目标 - 请参阅这些问题(无解决方案):Redirect https to http using rewrite rule in webconfig filehttps://stackoverflow.com/questions/15214717/iis-rewrite-https-to-http-whilst-keeping-existing-https-rules

我已将重写模块添加到 IIS,并在 web.config 中尝试了以下操作:

<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

但它仍然允许用户使用 https 访问非 https 站点(本质上是访问不同的站点)。

如何强制所有 https 请求为 http 请求?

编辑:我也尝试了所有建议的解决方案here,但没有成功。 url rewrite模块在IIS上肯定安装成功了!

edit2:尝试了以下但没有成功:

<system.webServer>
<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="^(?:www)?\.test.site\.com$"
            negate="true" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
            redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

我重新启动了 IIS,重写规则反映在 inetmgr 中。加载 https://test.site.com/ 时仍会加载 https。

【问题讨论】:

  • 您是否可以选择向服务器添加额外的 IP 地址以便更改绑定?
  • 有那个选项,但我想让 URL 重写按预期工作
  • 询问 IP 地址,因为似乎 URL 重写模块旨在仅在将请求分配给 IIS 站点后处理处理,因为匹配 URL 在主机之后开始。我认为IP地址最好,但没有它也可以完成。请参阅下面的答案。

标签: asp.net http iis https url-rewrite-module


【解决方案1】:

有几件事。首先,重写需要在 HTTPS 开启而不是关闭时进行处理。其次,对于需要通过 HTTPS 运行的应用程序,您需要将其从重写中排除。修改后的重写规则应如下所示:

<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" 
            negate="true" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" 
            redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

这应该将https://example.com/login 保留在 https 上,并且所有其他 URL 将被重定向到 http。例如,https://test.example.com/login 将被重定向到 http://test.example.com/login。需要将此重写规则放置在具有 HTTPS 绑定的站点上,才能使重写正常工作。

请注意,使用 301 永久重定向时,某些浏览器不会在后续命中时向服务器发出请求,因此在更改规则后需要清除浏览器缓存。网络选项卡甚至可能会说请求已发出,但 Fiddler 或 Wireshark 等外部工具会让您确定。

【讨论】:

  • 非常感谢 beavel!这些站点在同一个域上:https://www.site.com/http://test.site.com/ - 这会是个问题吗?如果有什么需要改变的话,你能不能更新你的答案。我很快就会测试..
  • 这应该不是问题,因为它将匹配所有不是 www.site.com 的内容。如果您的用户希望以 site.com 的形式访问 www.site.com,则该模式需要像这样:^(?:www)?\.site\.com$,这将使 www 成为可选。
  • 我更新了我的问题。该模式是否考虑了.com 之后的路径? https://test.site.com/login
  • @user982119 我已经更新了我的答案以反映新的模式和示例。请参阅我关于测试的警告。
  • 清除缓存。它不会重定向。
猜你喜欢
  • 2013-06-14
  • 2014-06-30
  • 2017-04-30
  • 2011-05-04
  • 2016-11-17
  • 2018-05-05
  • 1970-01-01
相关资源
最近更新 更多