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

问题描述 投票:3回答:1

我的问题正是提出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。

asp.net http iis https url-rewrite-module
1个回答
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这样的外部工具会让您确定。

© www.soinside.com 2019 - 2024. All rights reserved.