网站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.  

  

这是我的另一个博客,欢迎访问

posted @ 2018-08-30 14:40  顾星河  阅读(211)  评论(0编辑  收藏  举报