ASP.NET - 将所有https请求重写为http

时间:2013-11-19 16:11:18

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

我的问题正是提出here的问题,我决定尝试将所有https请求重写为http。我经过长时间的努力搜索,但似乎没有明确的方法来实现这一目标 - 请参阅这些问题(没有解决方案):Redirect https to http using rewrite rule in webconfig file; https://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重写模块肯定已成功安装在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。

1 个答案:

答案 0 :(得分: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上保留https://example.com/login,其他所有网址都会重定向到http。例如,https://test.example.com/login将重定向到http://test.example.com/login。需要将此重写规则放在具有HTTPS绑定的站点上,以使重写正常工作。

请注意,在使用301永久重定向时,某些浏览器不会在后续命中时向服务器发出请求,因此在更改规则后,需要清除浏览器缓存。网络选项卡甚至可以说谎并说出请求,但是像Fiddler或Wireshark这样的外部工具会让你知道。