使用完整 url 时,重定向规则 http 到 https 不起作用

Redirect rule http to https not working when using full url

我的 Azure 云服务在配置文件中同时启用了 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 ,it is correctly redirecting to https://mysite.kom

但是如果我输入 http://mysite.kom/other/default.aspx it is redirecting to http://mysite.kom/other/default.aspx instead of https://mysite.kom/other/default.aspx

我在我的 application_start 中添加了 GlobalFilters.Filters.Add(new RequireHttpsAttribute()); event.But 仍然是“http://mysite.kom/other/default.aspx没有被重定向 到“https://mysite.kom/other/default.aspx

我尝试了 Whosebug.I 中的所有类似问题,甚至将此规则移到了我的规则配置的顶部。使这个工作的任何建议

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

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 事件

这是我使用的:

<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/