В 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();
}
}