asp.net mvc 4 - Redirect certain pages from HTTPS to HTTP -


Google has indexed some of our pages with HTTPS and HTTP. I would like to redirect people who do not have HTTPS

This is an EPiServer 7 site, but MVC is essentially.

The following I have in my controllers ..

  if (currentPage.RequireSsl = null & amp ;! & amp; currentPage.RequireSsl.Value & amp; & amp ;! HttpContext.Request.IsSecureConnection) {if (HttpContext.Request.Url = null!) {this.RedirectPermanent (HttpContext come. Request.Url.ToString (return). replace ( "http:", "https:") ); }} Else if (HttpContext.Request.IsSecureConnection & amp; & amp; (currentPage.RequireSsl == null || currentPage.RequireSsl.Value == false)) {if (! HttpContext.Request.Url = null) {return this .RedirectPermanent (HttpContext.Request.Url.ToString (). Change ("https:", "http:")); }}  

Now, what do I need if the "secure" pages are requested for non-https, then it is 301 to https (when seen in Fiddler).

  ** Get GET http://domainxx.com/section/securepage/301 permanently from https://domainxx.com/section/securepage/**  

, however, if I request a non-secure page on HTTPS, it redirects, but I get 200 status codes instead of 301. Fiddler does not even list directly:

  ** GET http: // Domainxx / section / notsecurepage / 200 OK (text / html) **  

Create a new authorization filter as this

  public class CustomRequireHttpsAttribute:. FilterAttribute, IAuthorizationFilter {public void OnAuthorization (AuthorizationContext filterContext) {// abortion if it is a secure connection, you do not (filterContext.HttpContext.Request !. Aissikyuarknekshn) return; A [RequireHttps] attribute controller is applied to action // abortion if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes (typeof (RequireHttpsAttribute), true) .Length & gt; 0) return; If (filterContext.ActionDescriptor.GetCustomAttributes is true) (RequiredHttpsAttribute), Lang & gt; 0) returns; // abortion if it is not GET requests - we do not want to be redirected to a form post, return, (String.Equals (filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)!) // redirect to url string URL = "http: //" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl; FilterContext.Result = New Redirect (URL); }}  

then register it inside FilterConfig Redirect to http protocol if required The requested action filter Add (New CustomRequireHttpsAttribute ());

You have to add the [RequireHttps] functions / controllers where you will need the https


Comments