0
我想將我的頁面從http重定向到https協議,但遇到困難。以下是我試過到目前爲止:http to 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輸入網址效果很好。任何幫助將不勝感激。