---
title: "Url Rewrite Filter 简单教程 "
author: "meetrice"
date: "2007-11-02T17:38:53Z"
tags: []
slug: "url-rewrite-filter"
draft: false
lastmod: "2007-11-02T17:38:53Z"
id: 137628
url: "https://meetrice.iteye.com/blog/137628"
---

## [Url Rewrite Filter 简单教程](http://cngump.iteye.com/blog/56621 "永久链接：Url Rewrite Filter 简单教程")

Url Rewrite Filter 是一个基于java的 URL rewrite 一个包。 使用它后就可以使用一些友好的URL来代替 ?&组成的URL了。 例如可以把 [http://www.cngump.com/world/china/guangzhou](http://www.cngump.com/world/china/guangzhou) 转换为 [http://www.cngump.com/world.jsp?country=china&city=guangzhou](http://www.cngump.com/world.jsp?country=china&amp%3Bcity=guangzhou)  
步骤如下：  
1\. 下载和安装 Tomcat  
2.  部署一个空白的 Java Web Application.    urlrewrite.war    
3.  下载  Url Rewrite Filter [http://tuckey.org/urlrewrite/](http://tuckey.org/urlrewrite/)  
4.  解压 下载后的 urlrewritefilter-2.6.zip 到 TOMCAT\_HOME\\webapps\\urlrewrite\\ 下  
5.  修改 web.xml 如下

   ＜?xml version=”1.0″ encoding=”UTF-8″?＞  
＜web-app id=”WebApp\_9″ version=”2.4″ xmlns=”[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)”  
         xmlns:xsi=”[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”  
         xsi:schemaLocation=”[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee) [http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)“＞

    ＜display-name＞Url Rewrite＜/display-name＞

    ＜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＞  
   

＜/web-app＞  
6\. 在更目录新建一个 world.jsp 文件。代码如下：  
＜html＞  
 ＜header＞  
  ＜title＞world＜/title＞  
 ＜/header＞  
 ＜body＞  
 ＜%  
        String country = request.getParameter(”country”);  
        String city = request.getParameter(”city”);  
        out.write(”Country=” + country);  
        out.write(”＜br＞city=” + city);  
    %＞

 ＜/body＞  
＜/html＞

7\. 修改 WEB-INF 下的 urlrewrite.xml: (使用正则式进行解释)

＜urlrewrite＞  
＜rule＞  
    ＜from＞/world/(\[0-9]+)/(\[0-9]+)＜/from＞  
    ＜to＞/world.jsp?country=$1&city=$2＜/to＞  
＜/rule＞

＜/urlrewrite＞

8.  重新加载 urlrewrite 后测试:

[http://localhost:8080/urlrewrite/world/china/guangzhou](http://localhost:8080/urlrewrite/world/china/guangzhou)  
和[http://localhost:8080/urlrewrite/world.jsp?country=china&city=guangzhou](http://localhost:8080/urlrewrite/world.jsp?country=china&amp%3Bcity=guangzhou)效果一样