0

Google已經使用HTTPS和HTTP將我們的一些頁面編入索引。我想將不應該是HTTPS的頁面重定向回HTTP(301)。將HTTPS中的某些頁面重定向到HTTP

這是一個EPiServer 7站點,但本質上是MVC。

我有我的控制器下面..

  if (currentPage.RequireSsl != null && currentPage.RequireSsl.Value && !HttpContext.Request.IsSecureConnection) 
     { 
      if (HttpContext.Request.Url != null) 
      { 
       return this.RedirectPermanent(HttpContext.Request.Url.ToString().Replace("http:", "https:")); 
      } 

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

現在,我想要做什麼,如果「安全」頁面請求非HTTPS,它301S爲https(在拉琴查看時)。

**GET http://domainxx.com/section/securepage/ 
301 Moved Permanently to https://domainxx.com/section/securepage/** 

不過,如果我要求對HTTPS非安全頁面,將其重定向,但我得到一個200個狀態碼,而不是301提琴手甚至沒有列出直接:

**GET http://domainxx/section/notsecurepage/ 
200 OK (text/html)** 
+0

OK,其實這是OK ..提琴手ISN沒有選擇HTTPS請求,但http://www.howto301redirect.com/301-redirect-checker/是,並且工作正常。 – mp3duck

回答

1

按如下所示創建新的授權過濾器。

public class CustomRequireHttpsAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public void OnAuthorization(AuthorizationContext filterContext) 
    { 
     // abort if it's not a secure connection 
     if (!filterContext.HttpContext.Request.IsSecureConnection) return; 

     // abort if a [RequireHttps] attribute is applied to controller or action 
     if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 
     if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 

     // abort if it's not a GET request - we don't want to be redirecting on a form post 
     if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) return; 

     // redirect to HTTP 
     string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl; 
     filterContext.Result = new RedirectResult(url); 
    } 
} 

然後FilterConfig內註冊爲如下

//redirect to http protocol if RequiredHttps not assigned to the requested action 
filters.Add(new CustomRequireHttpsAttribute()); 

,你將不得不增加[RequireHttps]到行動/控制器,你需要https協議

+0

不知道這是否會在我的情況下工作,因爲有問題的控制器提供任何數量的URL,基於EPiServer頁面類型..因此我無法將屬性添加到我的操作中... – mp3duck

相關問題