【问题标题】:Redirect rule http to https not working when using full url使用完整网址时,将规则 http 重定向到 https 不起作用
【发布时间】:2017-02-13 06:01:07
【问题描述】:

我的天蓝色云服务在配置文件中启用了 http 和 https。 我尝试在我的 web.config 中添加此规则

<rule name="RedirectHTTPToHTTPS" stopProcessing="true">
        <match url="(.*)" ignoreCase="true" />
        <conditions>
            <add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />

        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>

如果我使用 http://mysite.kom ,它会正确重定向到 https://mysite.kom

但如果我输入http://mysite.kom/other/default.aspx,它会重定向到http://mysite.kom/other/default.aspx 而不是 https://mysite.kom/other/default.aspx

我在 application_start 事件中添加了GlobalFilters.Filters.Add(new RequireHttpsAttribute());。但“http://mysite.kom/other/default.aspx”仍然没有被重定向到“https://mysite.kom/other/默认.aspx"

我在 stackoverflow 中尝试了所有类似的问题。我什至将这条规则移到了我的规则配置的顶部。有什么建议可以使这个工作

【问题讨论】:

    标签: asp.net azure web-config http-redirect


    【解决方案1】:

    这是我使用的:

    <rewrite>
        <rules>
            <rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
        </rules>
    </rewrite>
    

    虽然您没有保留查询字符串,但您似乎仍然拥有获得 http->https 重定向所需的一切。这让我想知道浏览器缓存是否会给您带来问题?我之前遇到过这个问题,我认为它不起作用,因为我在启用 http->https 重定向之前点击了该站点,结果是我的浏览器从缓存中提供了页面。

    我将它包装在一个站点扩展中,它使用上述重写规则,所以我知道它有效: http://www.siteextensions.net/packages/RedirectHttpToHttps/

    【讨论】:

      【解决方案2】:

      为了解决这个问题,我添加了

      if (!HttpContext.Current.Request.IsSecureConnection && !HttpContext.Current.Request.Url.Host.Contains("localhost"))
              {
                  Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
                                               + HttpContext.Current.Request.RawUrl);
              }
      

      到我的 global.asax.cs 文件中的 Application_BeginRequest 事件

      【讨论】:

        猜你喜欢
        • 2015-08-10
        • 1970-01-01
        • 2017-11-19
        • 2013-06-02
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        相关资源
        最近更新 更多