UrlRewriteFilter
Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.
使用非常简单:
1,把 urlwrite-2.4.jar 放置到 CLASSPATH 中,比如 WEB-INF/lib
2,修改webapp的部署描述文件 web.xml
| <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </init-param> <init-param> <param-name>statusEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>statusPath</param-name> <param-value>/rewrite-status</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/show/*</url-pattern> </filter-mapping> |
3,在 WEB-INF 下建立文件 urlrewrite.xml
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.4//EN" "http://tuckey.org/res/dtds/urlrewrite2.4.dtd" > <urlrewrite> <rule> <from>/show/u([0-9]+).html</from> <to>/user.nv?m=show&u=$1</to> </rule> <rule> <from>/show/t([0-9]+),([0-9]+).html</from> <to>/thread.nv?m=show&t=$1&page=$2</to> </rule> </urlrewrite |
这将会把:
/user.nv?m=show&u=96 转化成 /show/u96.html
/thread.nv?m=show&t=7879&page=3 转换成 /show/t7879,3.html
This entry was posted
on Sunday, June 12th, 2005 at 18:15 and is filed under TechNotes.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.