---
title: "使用URLRewrite 实现URL伪静态化2"
author: "sameseam"
date: "2010-02-21T14:58:00Z"
tags: []
slug: "urlrewrite-url-2"
draft: false
lastmod: "2013-05-06T13:21:13Z"
id: 1860373
url: "https://sameseam.iteye.com/blog/1860373"
---

是否看到别人的网站网址名都不带后缀名比较酷，比如qq空间的地址，其实用urlrewrite这个包很容易就实现了。  
下面是使用说明：

1.下载urlrewrite,官方下载地址：[http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip](http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip)

2.解压缩文件，压缩包内文件copy到项目中(压缩包位置 -&gt; 项目位置)：

urlrewrite-2.6.0-src/webapp/WEB-INF/lib/urlrewrite-2.6.0.jar -&gt; WebRoot/WEB-INF/lib/urlrewrite-2.6.0.jar

urlrewrite-2.6.0-src/webapp/WEB-INF/urlrewrite.xml -&gt; WebRoot/WEB-INF/urlrewrite.xml  
3.将以下代码添加到web.xml里

Xml代码 [![复制代码](http://www.javaeye.com/images/icon_copy.gif)](http://www.javaeye.com/topic/255064 "复制代码")

01. **&lt;filter&gt;**
02. **&lt;filter-name&gt;**UrlRewriteFilter**&lt;/filter-name&gt;**
03. **&lt;filter-class&gt;**
04. org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
05. **&lt;/filter-class&gt;**
06. **&lt;/filter&gt;**
07. **&lt;filter-mapping&gt;**
08. **&lt;filter-name&gt;**UrlRewriteFilter**&lt;/filter-name&gt;**
09. **&lt;url-pattern&gt;**/\***&lt;/url-pattern&gt;**
10. **&lt;/filter-mapping&gt;**

```
<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.修改urlrewrite.xml

Xml代码 [![复制代码](http://www.javaeye.com/images/icon_copy.gif)](http://www.javaeye.com/topic/255064 "复制代码")

01. **&lt;?xml**version="1.0"encoding="utf-8"**?&gt;**
02. &lt;!DOCTYPEurlrewritePUBLIC"-//tuckey.org//DTDUrlRewrite2.6//EN"
03. "http://tuckey.org/res/dtds/urlrewrite2.6.dtd"**&gt;**
04. &lt;!--
05. ConfigurationfileforUrlRewriteFilter
06. http://tuckey.org/urlrewrite/
07. --**&gt;**
08. **&lt;urlrewrite&gt;**
09. **&lt;rule&gt;**
10. **&lt;from&gt;**^/(\[a-z]+)$**&lt;/from&gt;**
11. **&lt;to**type="forward"**&gt;**/world.jsp?id=$1**&lt;/to&gt;**
12. **&lt;/rule&gt;**
13. **&lt;rule&gt;**
14. **&lt;from&gt;**^/world/(.\*)$**&lt;/from&gt;**
15. **&lt;to&gt;**/world.jsp?tid=$1**&lt;/to&gt;**
16. **&lt;/rule&gt;**
17. **&lt;rule&gt;**
18. **&lt;from&gt;**^/(.\*).html$**&lt;/from&gt;**
19. **&lt;to&gt;**/test1/$1.jsp**&lt;/to&gt;**
20. **&lt;/rule&gt;**
21. **&lt;outbound-rule&gt;**
22. **&lt;note&gt;**
23. Theoutbound-rulespecifiesthatwhenresponse.encodeURLiscalled(ifyouareusingJSTLc:url)
24. theurl/rewrite-statuswillberewrittento/test/status/.
25. Theaboveruleandthisoutbound-rulemeansthatendusersshouldneverseethe
26. url/rewrite-statusonly/test/status/bothinthierlocationbarandinhyperlinks
27. inyourpages.
28. **&lt;/note&gt;**
29. **&lt;from&gt;**/rewrite-status**&lt;/from&gt;**
30. **&lt;to&gt;**/test/status/**&lt;/to&gt;**
31. **&lt;/outbound-rule&gt;**
32. **&lt;/urlrewrite&gt;**

```
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN" 
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd"> 

<!-- 

Configuration file for UrlRewriteFilter 
http://tuckey.org/urlrewrite/ 

--> 
<urlrewrite> 
	<rule> 
		<from>^/([a-z]+)$</from> 
		<to type= "forward" >/world.jsp?id=$1</to> 
	</rule> 
	<rule>
	    <from>^/world/(.*)$</from>
	    <to>/world.jsp?tid=$1</to>
	</rule>
	<rule>
		<from>^/(.*).html$</from>
		<to>/test1/$1.jsp</to>
	</rule>
	
	<outbound-rule> 
		<note> 
		The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url) 
		the url /rewrite-status will be rewritten to /test/status/. 
		
		The above rule and this outbound-rule means that end users should never see the 
		url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks 
		in your pages. 
		</note> 
		<from>/rewrite-status</from> 
		<to>/test/status/</to> 
	</outbound-rule>
</urlrewrite> 
```

rule是url重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,可以为多个,()里是匹配的正则表达式.  
好了,在项目中新建world.jsp,启动tomcat,输入  
[http://localhost:8080/mysite/world/1](http://localhost:8080/mysite/world/1)

mysite是你的项目名

实际上访问的是[http://localhost:8080/mysite/world.jsp?tid=1](http://localhost:8080/mysite/world.jsp?tid=1)  
这样就简单的实现了伪静态的效果