您的位置主页 > 编程专区 > Java > 让tomcat服务器使用url rewrite

让tomcat服务器使用url rewrite

2009-07-22    文章来源:互联网    浏览次数:796

1. 到这里下载:
http://tuckey.org/urlrewrite/urlrewritefilter-3.0.4
2. 解压urlrewritefilter-3.0.4.zip
里面包含几个文件

WEB-INF
–lib\urlrewrite-3.0.4.jar
     –urlrewrite.xml

按照目录位置放在工程的WEB-INF文件夹中
3. WEB-INF/web.xml配置UrlRewriteFilter
<servlet-mapping> 前面加入

      <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>
4. WEB-INF/urlrewrite.xml 中写入rewrite规则
加入诸如此类的正则表达规则:
Redirect one url
         <rule>
             <from>/some/old/page.html</from>
             <to type="redirect">/very/new/page.html</to>
         </rule>

Redirect a directory
         <rule>
             <from>/some/olddir/(.*)</from>
             <to type="redirect">/very/newdir/$1</to>
         </rule>

Clean a url
         <rule>
             <from>/products/([0-9]+)</from>
             <to>/products/index.jsp?product_id=$1</to>
         </rule>
     eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.

Browser detection
         <rule>
             <condition name="user-agent">Mozilla/[1-4]</condition>
             <from>/some/page.html</from>
             <to>/some/page-for-old-browsers.html</to>
         </rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.

5.重启tomcat,完成

例如:
在在WEB-INF/urlrewrite.xml 中写入rewrite规则为:
<rule>
      <from>gallery/t1.html</from>
      <to>epg/template/gallery,GalleryHome.vmx</to>
</rule>

<rule>
      <from>watch/([0-9]+).html</from>
      <to>epg/template/play,Watch.vmx?actid=$1</to>
</rule>

键入:
       http://www.bocoo.com/CN/gallery/t1.html就进入
       http://www.bocoo.com/CN/ epg/template/gallery,GalleryHome.vmx

       http:// www.bocoo.com /CN/watch/108.html就进入
       http:// www.bocoo.com /CN/epg/template/play,Watch.vmx?actid=108

  • 上一篇:js 过滤空格函数
  • 下一篇:没有了
  • 文章评论(查看全部)

    验证码: 中评 好评 差评