繁体   English   中英

HTTP到https重定向不起作用

[英]http to https redirect not working

我想将页面从http重定向到https协议,但是遇到了麻烦。 到目前为止,这是我尝试过的:

将以下内容添加到global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (!Request.IsLocal && !Request.IsSecureConnection)
        if (HttpContext.Current.Request.IsSecureConnection || HttpContext.Current.Request.IsLocal)
            Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}

在每个页面中添加以下内容

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    if (!IsPostBack)
        RedirectAccordingToRequiresSSL();
}

private void RedirectAccordingToRequiresSSL()
{
    if (!Request.IsLocal && !Request.IsSecureConnection)
        if (!Request.IsSecureConnection) // Need to redirect to https
            RedirectAccordingToRequiresSSL(Uri.UriSchemeHttps);
}

private void RedirectAccordingToRequiresSSL(string scheme)
{
    var url = scheme + Uri.SchemeDelimiter + Request.Url.Authority + Request.Url.PathAndQuery;
    Response.Redirect(url, false);
}

将重写添加到web.config,但是我认为重写元素本身无法正常工作

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
      </rules>
    </rewrite>

不管我尝试什么,在http://my.website.here中输入都不会重定向到https://my.website.here 我不断收到的错误是403(禁止)。 直接用https输入URL效果很好。 任何帮助将不胜感激。

我只是在本地尝试过,可以使用以下规则从http重定向到https。

enter code here
<rule name="TO SSL" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="Off" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{URL}" />
</rule>

提琴手跟踪示例

GET http://www.site.com/test?qs=1 HTTP/1.1
Host: www.site.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36


HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: https://www.site.com/test?qs=1
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 12 Feb 2014 01:14:49 GMT
Content-Length: 154

暂无
暂无

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

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