[英]Redirect from HTTP to HTTPS and to a new domain with C# MVC in IIS7.5
[英]How to redirect HTTP to HTTPS in MVC application (IIS7.5)
我需要将我的 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>
你可以在代码中做到这一点:
全局.asax.cs
protected void Application_BeginRequest(){
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
或者您可以将相同的代码添加到操作过滤器:
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);
}
}
}
在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"));
}
}
301重定向:SEO最佳实践(搜索引擎优化)
301 Moved Permanently
重定向状态响应代码被认为是将用户从 HTTP 升级到 HTTPS 的最佳实践(请参阅 Google 推荐)。
因此,如果 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();
}
}
我在 Global.asax 中使用以下内容:
protected void Application_BeginRequest()
{
if (FormsAuthentication.RequireSSL && !Request.IsSecureConnection)
{
Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
}
}
我在 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>
对于简单的情况,您可以使用 RequireHttpsAttribute。
[RequireHttps]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
如 MSDN 中所述...
“表示强制通过 HTTPS 重新发送不安全的 HTTP 请求的属性。”
不过,我不确定您是否愿意使用它在大型网站上强制执行 HTTPS。 有很多装饰要做,还有机会错过控制器。
在 web.config 文件中使用此代码将 http:// 重定向到 https://
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS force" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer></configuration>
这很简单。 只需在“Global.asax”文件中添加一行,如下所示:
protected void Application_Start()
{
GlobalFilters.Filters.Add(new RequireHttpsAttribute(true));
}
如果您只想应用服务器端,而不是本地端,请应用以下代码:
protected void Application_Start()
{
if (!HttpContext.Current.Request.IsLocal)
GlobalFilters.Filters.Add(new RequireHttpsAttribute(true));
}
希望对您有所帮助:)谢谢!
我是这样做的,因为本地调试会话使用自定义端口号:
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection)
{
if (HttpContext.Current.Request.IsLocal)
{
Response.Redirect(Context.Request.Url.ToString().Replace("http://localhost:25885/", "https://localhost:44300/"));
}
else
{
Response.Redirect(Context.Request.Url.ToString().Replace("http://", "https://"));
}
}
}
最好有某种方法以编程方式获取 URL 和 SSL URL ...
仅当网站在服务器上启动时强制使用 https,并在您的机器上运行网站进行开发时忽略它:
在 Global.asax 中:
你需要 Application_BeginRequest() 方法
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// .....
}
//force https on server, ignore it on local machine
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection && !Context.Request.Url.ToString().Contains("localhost"))
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
}
这个答案并不完全适用于 OP,但对于那些不能像我一样工作并且遇到过这个问题的人(虽然我知道 OP 中有 403 而不是 404 错误),如果你得到 404,请参考这个答案: https://stackoverflow.com/a/6962829/5416602
请检查您的 IIS 中是否绑定了 HTTP 端口 (80) 而不仅仅是 HTTPS 端口 (443)
我无法添加评论,但认为此补充信息可能会对某些人有所帮助。
我用 301 永久重定向实现了 Global.asax 的想法,并在 IIS 中向网站添加了 http 绑定。 它仍然给了我 403 Forbidden,直到我记得在 SSL 设置中取消勾选“需要 SSL”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.