官网
http://tuckey.org/urlrewrite/#download
概念
UrlRewrite 地址重写,可以将非静态页面地址进行转换,伪装成html后缀的网页。
优点
提高安全性,可以有效的避免一些参数名、ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规则的话直接会返回个404或错误页面,这比直接返回500或一大堆服务器错误信息要好的多;
美化URL,去除了那些比如*.do之类的后缀名、长长的参数串等,可以自己组织精简更能反映访问模块内容的URL;
更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息;
转换规则
rule说明
通过正则表达式对请求进行匹配
实际请求地址
^/m/category_([0-9]+)/content_([0-9]+).html$
/main.jsp?categoryid=$1&contentid=$2
outbound-rule说明
页面显示的链接规则表达式
转换为实际显示的地址
^/main.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/m/category_$1/content_$2.html
rule与outbound-rule需要匹配。
注意转义字符:
& 转义为 &
? 转义为 /?
示例代码
1.下载项目jar包并加载到项目工程中。
2.将urlrewrite.xml文件加载到WEB-INF目录下,并编写转换规则,编写规则使用正则表达式。
示例代码
/p>
"http://tuckey.org/res/dtds/urlrewrite3.1.dtd">
^/index.html$
/index.jsp
^/login.html$
/login2.jsp
^/query.html$
/queryResult.jsp
^/m/category_([0-9]+).html$
/main.jsp?categoryid=$1
^/m/category_([0-9]+)/content_([0-9]+).html$
/main.jsp?categoryid=$1&contentid=$2
^/v/category_([0-9]+)/content_([0-9]+).html$
/view.jsp?categoryid=$1&contentid=$2
^/f/category_([0-9]+)/pageNow_([0-9]+).html$
/frame.jsp?categoryid=$1&pageNow=$2
^/p/category_([0-9]+)/pageNow_([0-9]+).html$
/proframe.jsp?categoryid=$1&pageNow=$2
^/c/category_([0-9]+)/pageNow_([0-9]+).html$
/credentails.jsp?categoryid=$1&pageNow=$2
^/p/depart_([0-9]+).html$
/people.jsp?departid=$1
^/index.jsp
/index.html
^/login2.jsp
/login.html
^/queryResult.jsp
/query.html
^/main.jsp\?categoryid=([0-9]+)$
/m/category_$1.html
^/main.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/m/category_$1/content_$2.html
^/view.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/v/category_$1/content_$2.html
^/frame.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/f/category_$1/pageNow_$2.html
^/proframe.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/p/category_$1/pageNow_$2.html
^/credentails.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/c/category_$1/pageNow_$2.html
^/people.jsp\?departid=([0-9]+)$
/p/depart_$1.html
3.在web.xml文件中进行声名。
UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
UrlRewriteFilter
/*
REQUEST
FORWARD
4.Jsp页面链接声名方式
说明:按照API的指引,java里面编写的链接应该使用转义字符 & 代替 & 才对,但是我使用了&反而不起作用,使用&才行,所以需要大家多测试一下,而且我在本机上部署访问,显示的链接规则是不生效的(即:无法显示 /m/category_1/content_2.html ),但是可以通过这样的规则去正常访问,反而部署在服务器上就正常显示。
测试结果
访问http://xxx.com/index.html 实际访问到的是index.jsp页面
参考案例
http://pihai.iteye.com/blog/384386
http://blog.sina.com.cn/s/blog_7a2356a60100x4fy.html
http://blog.sina.com.cn/s/blog_a72be0600101dwxl.html