LLM*_*LLM 50 asp.net-mvc https redirect http
我需要将我的HTTP站点重定向到HTTPS,添加了下面的规则,但是当我尝试使用http://www.example.com时我得到403错误,当我在浏览器中键入https://www.example.com时,它工作正常.
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Chr*_*ken 110
你可以在代码中做到:
的Global.asax.cs
protected void Application_BeginRequest(){
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
Run Code Online (Sandbox Code Playgroud)
或者您可以将相同的代码添加到操作过滤器:
public class SSLFilter : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext){
if (!filterContext.HttpContext.Request.IsSecureConnection){
var url = filterContext.HttpContext.Request.Url.ToString().Replace("http:", "https:");
filterContext.Result = new RedirectResult(url);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ier 44
在Global.asax.cs:
简单的重定向
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection
&& !Context.Request.IsLocal // to avoid switching to https when local testing
)
{
// Only insert an "s" to the "http:", and avoid replacing wrongly http: in the url parameters
Response.Redirect(Context.Request.Url.ToString().Insert(4, "s"));
}
}
Run Code Online (Sandbox Code Playgroud)
301重定向:SEO最佳实践(搜索引擎优化)
该301 Moved Permanently重定向状态响应代码被认为是最好的做法升级从HTTP用户HTTPS(见谷歌的建议).
因此,如果Google或Bing机器人也将被重定向,请考虑以下因素:
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection
&& !Context.Request.IsLocal // to avoid switching to https when local testing
)
{
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", Context.Request.Url.ToString().Insert(4, "s"));
Response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我在Global.asax中使用以下内容:
protected void Application_BeginRequest()
{
if (FormsAuthentication.RequireSSL && !Request.IsSecureConnection)
{
Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
}
}
Run Code Online (Sandbox Code Playgroud)
我在 Web.config 文件中有以下 ASP.NET MVC 重写规则:
您可以使用 web.config 文件尝试此代码。如果您的 URL 是http://www.example.com那么它将被重定向到此 URL https://www.example.com。
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>Run Code Online (Sandbox Code Playgroud)