使用url rewrite重写

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%2CGalleryHome.vmx</to>

 </rule>

 

 <rule>

     <from>watch/([0-9]+).html</from>

     <to>epg/template/play%2CWatch.vmx?actid=$1</to>

 </rule>

 

键入:http://localhost:8000/CN/gallery/t1.html就进入

      http://localhost:8000/CN/ epg/template/gallery%2CGalleryHome.vmx

 

      http://localhost:8000/CN/watch/108.html就进入

      epg/template/play%2CWatch.vmx?actid=108

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值