【问题标题】:IIS Rewrite Rule in web.config to redirect HTTPS requests to HTTPweb.config 中的 IIS 重写规则将 HTTPS 请求重定向到 HTTP
【发布时间】:2014-11-04 04:42:59
【问题描述】:

我需要将所有https请求重定向到http,例如如果有人访问https://www.example.com/another-page/http://www.example.com/another-page/

我的 web.config 现在有以下重写规则,但它不能正常工作。它将https://www.example.com/another-page/ 重定向到https://www.example.com/,因此重定向到站点的根目录,但我希望重定向保持在同一个URL 中,并且只将https 重写为http。

 <rule name="Redirect to HTTP" stopProcessing="true">
   <match url="(.*)" />
     <conditions>
       <add input="{R:1}" pattern="^onepage/(.*)$" negate="true" />
       <add input="{HTTPS}" pattern="^ON$" />
     </conditions>
     <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
 </rule>

任何有关更改上述规则以使其仅将 https 更改为 http,但保留完整 url 访问的帮助将不胜感激!

【问题讨论】:

标签: iis url-rewriting web-config


【解决方案1】:

我设置了你的规则,清理了一点,它起作用了;所以这并没有真正回答很多新问题。

建议:删除onepage 输入条件仅用于测试,正如问题评论中所建议的cheesmacfly。

另外,请尝试将操作更改为 {R:1} 而不是 {R:0}。在这种情况下应该没关系,但我只是喜欢使用 1 及以上来匹配特定的捕获组。 R:0 表示整个匹配的字符串,这总是让我有点困惑。

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

一种可能是您的浏览器缓存了您之前的规则尝试。当redirectType 为Permanent,并且您仍在开发或测试时,浏览器通常会缓存以前的规则。清除您的浏览器缓存,和/或删除永久,和/或以隐身模式浏览。完成测试后,将其更改为永久。请参阅此答案中的第 2 和第 3:https://stackoverflow.com/a/9204355/292060

【讨论】:

    【解决方案2】:

    请将以下代码粘贴到web.config 文件中。

    <rule name="Redirect to http" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP}" pattern="off" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="http://{HTTPS_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
    </rule>
    

    【讨论】:

    • 谢谢,但正如问题中所述,站点在 IIS 而不是 Apache 上运行,因此没有 .htaccess 文件。相反,我使用的是 web.config 文件和 IIS 重写模块。
    • 我认为您输入了错误的 HTTP 和 HTTPS。它们必须颠倒
    猜你喜欢
    • 2013-06-02
    • 2015-08-10
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多