这东西简单,但还是记录下,好查。首先抄一下使用这东西的好处
一:提高安全性,可以有效的避免一些参数名、ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规则的话直接会返回个404或错误页面,这比直接返回500或一大堆服务器错误信息要好的多
二:美化URL,去除了那些比如*.do之类的后缀名、长长的参数串等,可以自己组织精简更能反映访问模块内容的URL
三:更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息
官方网站:http://tuckey.org/urlrewrite/
使用过的版本是3.2,够用了,也不想升级到4。maven项目的话增加依赖,非maven项目自己手动添加jar包。
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.2.0</version>
</dependency>
在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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
jar包中有个conf-dist.xml文件,复制出来改名为urlrewrite.xml放在WEB-INF目录下。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
<rule match-type="regex">
<note>
from: http://domain/{method}-{id}.html
to : http://domain/method.html?id=331
</note>
<from>^/(method)-(\d*)\.html$</from>
<to type="forward">/$1.html?id=$2</to>
</rule>
</urlrewrite>
使用正则,下面那个$1就是按(左)括号的位置来算,从1开始递增。
一般就用到<rule>规则,具体配置见下面抄录两段
其实,urlrewrite其实就是个过虑器,它将会过虑用户的所有请求,符合规则的便对其进行重定向,具体的配置参数的使用方法见官方文档:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
配置好web.xml后将下载的urlrewritefilter-3.2.0中的urlrewrite.xml配置文件放在WEB-INF目录下,这样在使用urlrewritefilter时它便自动到该目录下读取相关的配置了,我配置了个简单的,代码如下:
<rule>
<from>/zh_CN/NewsInfo/NEWS_([0-9]+)</from>
<to>/zh_CN/NewsInfo/view.do?method=view&id=$1</to>
</rule>
<outbound-rule>
<from>/zh_CN/NewsInfo.do/?method=view&id=([0-9]+)$</from>
<to>/zh_CN/NewsInfo/NEWS_$1</to>
</outbound-rule>
rule结点中form的规则默认使用的是正则表达式来匹配的,当用户访问服务器时的URL会与该配置相比较,如果符合规则就会按照下面to结点中的配置对其进行跳转,其默认是forward跳转,具体配置可见官网文档。
outbound-rule结点是服务器解析后的页面URL对外表现的形式,配置与上面的大体相同,如我一新闻链接代码在JSP中书写的形式如下:
<c:url var="news_url" value="/zh_CN/Press/view.do?method=view&id=${cur.id}&msg=Press" />
<li>· <a href="${news_url}" mce_href="${news_url}" target="_blank" title="${cur.titleSub}">${cur.title}</a></li>
经服务器解析后,最终显示在页面中的将变成:
<li>· <a href="/项目名/zh_CN/NewsInfo/NEWS_9999" mce_href="项目名/zh_CN/NewsInfo/NEWS_9999" target="_blank" title="${cur.titleSub}">新闻标题</a></li>
当用户点击此链接后urlrewrite便用通过rule的配置重跳转到真正的访问地址,这样便很好的隐藏了真实地址
这其中有点是需要注意的,引用官网中的一段话:
Using the example above JSP's with the code
<a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>
will output
<a href="/world/usa/nyc">nyc</a>
Or JSTL
<a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a>
will output
<a href="/world/usa/nyc">nyc</a>
Note, If you are using JSTL (ie, <c:url) this will work also.
意思就是说需要转化的链接不能直接写在a标签中,需要写在c:url或其他服务器需要解析的变量中,这样才能对其重新显示
<outbound-rule> element
Zero or more. This is very similar to a normal rule but it is used for rewriting urls that go through response.encodeURL()
.
Attribute | Possible Value | Explanation |
---|---|---|
enabled (optional) | true (default) | Enable this rule. |
false | Disable this rule. | |
encodefirst (optional) | false (default) | Run encodeURL() after running this outbound rule. |
true | Run encodeURL() before running this outbound rule. |
May contain "run", "from", "to" and "set" element(s) also. Example:
<outbound-rule> <from>^/world.jsp?country=([a-z]+)&city=([a-z]+)$</from> <to>/world/$1/$2</to> </outbound-rule>
Using the example above JSP's with the code
<a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>
will output
<a href="/world/usa/nyc">nyc</a>
Or JSTL
<a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a>
will output
<a href="/world/usa/nyc">nyc</a>
Note, If you are using JSTL (ie, <c:url) this will work also.