zoukankan      html  css  js  c++  java
  • ASP.NET在程序中设置301的方法。

    因为IIS设置301需要在服务器中配置很麻烦,所以ME选择了在程序中实现。
    程序中实现有个缺点就是执行效率没有在IIS服务器中速度快。

    当然了,这里说的只是适合动态网站的,如果都是.html静态文件就飘过吧!

    好了还是直接上代码吧:
    网页首页文件index.aspx后台代码

    //判断是否是www.开头,如果不是301调整到www.域名
    if (!System.Web.HttpContext.Current.Request.Url.ToString().StartsWith("http://www."))
    {
    //301 重定向到 /目录下
    HttpContext.Current.Response.StatusCode = 301;
    HttpContext.Current.Response.Status
    = "301 Moved Permanently";
    HttpContext.Current.Response.AddHeader(
    "Location", "http://www.qinquan.org/");
    HttpContext.Current.Response.End();
    }

    这里因为是我的独立站点,所以直接写www.了。如果是二级域名就需要根据需求自己修过了。


    栏目页/内容页代码:

    //如果url结尾不是以/符号结尾的,同样301到末尾增加/符号。
    if (!System.Web.HttpContext.Current.Request.RawUrl.EndsWith("/"))
    {
    //301 重定向到 /目录下
    HttpContext.Current.Response.StatusCode = 301;
    HttpContext.Current.Response.Status
    = "301 Moved Permanently";
    HttpContext.Current.Response.AddHeader(
    "Location", System.Web.HttpContext.Current.Request.RawUrl + "/");
    HttpContext.Current.Response.End();
    }
  • 相关阅读:
    tomcat中配置jmx监控
    常用sql
    String、StringBuffer、StringBuilder的不同使用场景
    求交集的几种方法
    使用liunx部署的心得
    几种有助于开发的注释方式。
    SpringDataJPA的几个使用记录
    今年要完成的几件事
    研究kisso跨域登录的心得
    SpringBoot使用的心得记录
  • 原文地址:https://www.cnblogs.com/chear/p/2041496.html
Copyright © 2011-2022 走看看