简体   繁体   English

HTTP 到 HTTPS 重定向 - IIS 8.5 无法正常工作

[英]HTTP to HTTPS Redirect - IIS 8.5 not working properly

I've read a number of posts here on SO as well as on the .net (IIS blogs, etc.).我在这里阅读了许多关于 SO 以及 .net(IIS 博客等)的帖子。 I'm trying to force all connections going from domain.com to www.domain.com and at the same time forcing the request from HTTP to HTTPS .我试图强制从 domain.com 到 www.domain.com 的所有连接,同时强制从 HTTP 到 HTTPS 的请求

I'm using this set of rules and rewrites but the only thing happening is that it's redirecting fine but not redirecting to SSL.我正在使用这组规则并重写,但唯一发生的事情是它重定向正常但没有重定向到 SSL。

<!-- Redirect to HTTPS -->
<rewrite>
    <rules>
        <rule name="Redirect to www" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="false">
                <add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain.com$" ignoreCase="true" negate="false" />
            </conditions>
            <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
        </rule>
    </rules>
    <rewriteMaps>
        <rewriteMap name="MapProtocol" defaultValue="http">
          <add key="on" value="https" />
          <add key="off" value="http" />
        </rewriteMap>
    </rewriteMaps>
</rewrite>

What am I doing wrong?我究竟做错了什么?

Main blog reference: http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action and this SO post - web.config redirect non-www to www主要博客参考: http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action和这篇 SO 帖子 - web.config 将非 www 重定向到 www

Edit: So I found this blog post: http://www.meltedbutter.net/wp/?p=231 and gave it a try and voila!编辑:所以我找到了这篇博文: http : //www.meltedbutter.net/wp/? p=231 并尝试了一下,瞧! Worked like a charm.像魅力一样工作。 Not sure why this worked over the rules posted above but in my case the below is working and successfully taking all non-www traffic and redirecting it to both www and https .不知道为什么这对上面发布的规则有效,但在我的情况下,下面的规则可以正常工作并成功获取所有非 www 流量并将其重定向到wwwhttps

<!-- Redirect to HTTPS -->
        <rewrite>
            <rules>
                <rule name="http to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://www.domain.com/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>

We faced same issue while redirecting from http to https using URL Rewrite module, But after switching off the require ssl module within IIS did the trick.我们在使用 URL Rewrite 模块从 http 重定向到 https 时遇到了同样的问题,但是在关闭 IIS 中的 require ssl 模块后,我们遇到了同样的问题。

  1. Go to SSL Settings of website转到网站的 SSL 设置
  2. Uncheck require SSL checkbox.取消选中需要 SSL 复选框。
  3. Apply settings and Restart iis应用设置并重启iis

在某些情况下,这是一个很长的镜头,但我已经删除了网站的端口 80 绑定,因为我只想要 SSL/端口 443。所以据我所知,端口 80 绑定也需要重写才能正常工作。

In my case, I did everything @Valien said and lots of other tries but it did not work.就我而言,我做了@Valien 所说的一切以及许多其他尝试,但没有奏效。 finally I found the problem.最后我发现了问题。 It was because I was using 2 rules.这是因为我使用了 2 条规则。 First rule was rewriting to NodeJs server and the second to redirect http to https. I changed the order and now it is working correctly.第一条规则是重写到 NodeJs 服务器,第二条是将 http 重定向到 https。我更改了顺序,现在它工作正常。

The config with the correct order in case more than 1 rule is applied:如果应用了超过 1 个规则,则具有正确顺序的配置:

<rewrite>
     <rules>
           <rule name="http to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://mywebsite.com/{R:1}" redirectType="SeeOther" />
           </rule>
           <rule name="ReverseProxyToLocalhost:3000" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
           </rule>
      </rules>
 </rewrite>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM