URL重写

什么是url重写:其实就是把url地址改了一下,

对什么样的URL地址重写:带参数的url进行重写

为什么对其重写:提高SEO优化,搜索引擎蜘蛛会根据url进行搜索,当爬到带参数的url时会把这个url的权重降低,因为参数会变,页面内容会变,所以要把带参数的url变成不带参数的url,其实并不是html权重高,只要不带参数,html,aspx权重一样高

例子:

aspx.cs

重写前:

       node.NavigateUrl = ListBook.aspx?categoryId=" + item.Id;

重写后

node.NavigateUrl = "/ListBook/ListBook_" + item.Id + ".aspx";

我们只要在请求管道的第一个事件里重写这个url就行了,因为只要是动态页面都会走请求管道的第一个事件,所以可以建一个Golble

然后在global文件的 Application_BeginRequest的事件里处理请求,因为这个是管道的第一个事件

在Application_BeginRequest里获取请求的url,

string url = Request.AppRelativeCurrentExecutionFilePath;//获取的是~/book_1.aspx

判断请求的url是否需要重写(用正则表达式),如果匹配成功进行重写:

         Match match= Regex.Match(url, @"~/BookList_(\d+).aspx");
         if (match.Success)
         {
             string categoryId = match.Groups[1].Value;//获得类别编号
             Context.RewritePath("/BookList.aspx?categoryId=" + categoryId);//重写url
         }

Application_BeginRequest代码:

 protected void Application_BeginRequest(object sender, EventArgs e)
        {
          string url =Request.AppRelativeCurrentExecutionFilePath;//~/BookList_26.aspx
            //判断用户请求的url是否是需要重写。
         Match match= Regex.Match(url, @"~/BookList_(\d+).aspx");
         if (match.Success)
         {
             string categoryId = match.Groups[1].Value;
             Context.RewritePath("/BookList.aspx?categoryId=" + categoryId);
         }

        }

 

 

 

 

 

posted @ 2013-05-03 11:58  卡西法儿  阅读(1033)  评论(3编辑  收藏  举报