網站301跳轉

  源碼:

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            if (request.Url.Scheme != "https")
            {
                Page301Url(response, "https://" + request.Url.Host + request.RawUrl);
            }
        }
        protected void Page301Url(HttpResponse response, string url301)
        {
            response.Clear();
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Cache.SetExpires(DateTime.UtcNow.AddYears(-1));
            response.Cache.SetMaxAge(TimeSpan.Zero);
            response.Cache.SetNoServerCaching();
            response.Cache.SetNoStore();
            response.Cache.SetNoTransforms();
            response.Cache.SetProxyMaxAge(TimeSpan.Zero);
            response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            response.AddHeader("Location", url301);
            response.End();
        }
View Code

  在Global裏寫好邏輯,網站運行的時候會首先進入Global,這裏介紹的是http如何跳https.  

  

這是我的另一個博客,歡迎訪問

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章