简体   繁体   English

IIS 10 URL 重写 http 流量到 Z5E056C500A1C4B6A7110 重定向BADED

[英]IIS 10 URL Rewrite http traffic to https redirect not working

IIS 10 server behind an AWS application load balancer will not redirect traffic for domain without www when client requests http rather than https.当客户端请求 http 而不是 https 时,AWS 应用程序负载均衡器后面的 IIS 10 服务器不会重定向没有 www 的域的流量。 The rule to redirect traffic when www is specified works fine, but 404 is returned if you try the same url without www.指定 www 时重定向流量的规则可以正常工作,但如果您尝试没有 www 的相同 url,则会返回 404。

So:所以:

  1. Enter "http://dname.com/blog" = 404输入“http://dname.com/blog”=404

  2. Enter "http://www.dname.com/blog" = redirect to "https://www.dname.com/blog"输入“http://www.dname.com/blog”=重定向到“https://www.dname.com/blog”

     <rule name="Force HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" /> <add input="{HTTP_HOST}" pattern="^dname\.com$" /> </conditions> <action type="Rewrite" url="https://www.dname.com{REQUEST_URI}" /> </rule> <rule name="Force WWW HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" /> <add input="{HTTP_HOST}" pattern="^www\.dname\.com$" /> </conditions> <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" /> </rule>

Nothing worked for me even after going through the answers provided on different forums.即使在浏览了不同论坛上提供的答案之后,我也没有任何效果。 After 2 days of banging my head in this here's what I found which solved the issue:经过两天的努力,这就是我发现的解决问题的方法:

  1. Bindings: Port 80 must be enabled (This can be added in bindings section in IIS).绑定:必须启用端口 80(可以在 IIS 的绑定部分添加)。

  2. SSL settings: Required SSL must be unchecked. SSL 设置:必须取消选中所需的 SSL。

  3. Add Rule:添加规则:

<rewrite>
    <rules>
        <rule name="http to https redirection" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                appendQueryString="false" />
        </rule>
    </rules>
</rewrite>
  1. Verify web config as it should reflect the rule added in IIS.验证 web 配置,因为它应该反映在 IIS 中添加的规则。

I don't know why the previously posted rules wouldn't work, but I was able to create a refined rule that is working:我不知道为什么以前发布的规则不起作用,但我能够创建一个有效的细化规则:

            <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^(www\.)?dname\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
            </rule>

The above rule combines the two rules instead of looking for the domain without the www and then with the www in a separate rule.上述规则结合了这两个规则,而不是在单独的规则中查找没有 www 的域,然后再查找带有 www 的域。 The regex (www\.) tells the rule to look for "www."正则表达式(www\.)告诉规则查找“www”。 and the question mark tells it that it may or may not be there, so that includes the domain with and without the www.问号告诉它它可能存在也可能不存在,因此包括带有和不带有 www 的域。

There is a very very important step that should take care, before setup a redirect configure.在设置重定向配置之前,有一个非常重要的步骤需要注意。

in web Sites project --> Actions(in the right) --> Bindings, the content will like below: Binding Content在 web Sites 项目中 --> Actions(in the right) --> Bindings,内容如下: Binding Content

You take carefully the yellow color part, the yellow part is your original web IP address.你小心黄色部分,黄色部分是你原来的web IP地址。 This original IP address must exist in "Site Bindings", without the yellow part the URL redirect will not working anymore .这个原始的 IP 地址必须存在于“站点绑定”中,没有黄色部分,URL 重定向将不再起作用

The following config is my current IIS URL redirect setting:以下配置是我当前的 IIS URL 重定向设置:

    <rewrite>
        <globalRules>
            <rule name="Http redirect to Https" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="localhost:8080" />   <-- the Red one should match above Site Bindings original IP address
                </conditions>
                <action type="Redirect" url="https://Your-Host-Name/{R:1}" redirectType="SeeOther" />
            </rule>
        </globalRules>
    </rewrite>

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

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