动态的URL变成静态的URL可以UrlRewriteFilter来处理

UrlRewriteFilter的介绍:
    UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如 Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。
   为什么要使动态的URL变成伪静态的URL:
   1:为了对搜索的友好,因为有些搜索不能抓取动态页面或是对动态抓取的页面没有静态页面高.
   2:屏蔽内部的url结构.
   3:美化url.
   UrlRewriteFilter使用:
     1.下载http://tuckey.org/urlrewrite/#download目前稳定的版本是2.6,最新版3.1,推荐使用2.6版.解压缩后将文件考到相应的web-inf/lib和web-inf下.
     2、配置web.xml
      <filter>
       <filter-name>UrlRewriteFilter</filter-name>
       <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>UrlRewriteFilter</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>


     根据自己的需要,将相应目录下的url转给UrlRewriteFilter来处理。
   3、配置urlwrite规则文件WEB-INF/urlrewrite.xml
  http://www.5a520.cn/book/116 会直接forward 到 http://www.5a520.cn/book.php?id-116 结果都是"创世传奇之魔族风云 _玄幻小说_小说520网"这个标题.
  http://www.5a520.cn/bookxuanhuan/3  会直接forward 到 http://www.5a520.cn/cata.php?id=bookxuanhuan&index=3 结果都是"玄幻小说_小说520网"这个标题.
配置如下:
 

<rule>
        <from>/book/([0-9]+)$</from>
        <to>/book.php?id=$1</to>
    </rule>

 <rule>
        <from>/book([a-z]+)$</from>
        <to>/cata.php?id=book$1</to>
 </rule>

 <rule>
        <from>/book([a-z]+)/([0-9]+)$</from>
        <to>/cata.php?id=book$1&amp;index=$2</to>
 </rule>


   注意:
   1.urlrewrite.xml是utf-8.所以如果你要在rule上加note标签为中文的话,也一定是要utf-8
   2.UrlRewriteFilter 最好是配置在web.xml的前面filter上,不然有可能对有些url转变失去作用.
   3.在写rule的时,如果有多个参数时,中间的连接符号&应该是&amp;

  下面对 urlrewrite.xml标签的一些说明:
  urlrewrite属性:有仅只有一个.
  rule属性::至少一个.

<name> 属性(可选)
    <rule>
    <name>World Rule</name>
    <from>^/world/([a-z]+)/([a-z]+)$</from>
    <to>/world.jsp?country=$1&amp;city=$2</to>
    </rule>
  <note>属性(可选)
    <rule>
    <name>World Rule</name>
    <note>
        Cleanly redirect world requests to JSP,
        a country and city must be specified.
        </note>
    <from>^/world/([a-z]+)/([a-z]+)$</from>
    <to>/world.jsp?country=$1&amp;city=$2</to>
    </rule>

    <condition>属性(可选)
    可以对时间,方法,来源,端口,类型等进行设置,如
    <condition name="user-agent" operator="notequal">Mozilla/[1-4]</condition> 客户端游览器不是Mozilla14版本以下可以访问.
   

<condition type="user-in-role" operator="notequal">bigboss</condition> 是bigboss不能访问.
    <condition name="host" operator="notequal">www.example.com</condition> 主机是www.example.com不能访问
    <condition type="method" next="or">PROPFIND</condition> 下个rule是PROPFIND可以访问
    <condition type="method">PUT</condition> 是put类型

    type属性:
    最主要就是 forward (default):在客户端URL是不转向的 redirect 在客户端URL是转向的,所以一般采用 forward
 set属性:这个有点像apache中的rewrite强大之处了.除了下面的设置client,还可以设置cookie,content-   type,charset,header,request
   

<rule>
    <condition name="user-agent">Mozilla/3/.0 (compatible; AvantGo .*)</from>
    <from>.*</from>
    <set name="client">AvantGo</set>
    </rule>
    <rule>
    <condition name="user-agent">UP/.Browser/3.*SC03 .* </from>
    <from>.*</from>
    <set name="client">Samsung SCH-6100</set>
    </rule>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值