您的位置:首页 > 其它

UrlRewriteFilter 学习笔记

2014-05-12 18:00 471 查看
1、简介

UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。

其主页:http://tuckey.org/urlrewrite/

2、安装

在其主页下载该包文件,把其jar 包放在lib 目录下,在web.xml 中添加下面内容

Xml代码

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

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

在 WEB-INF目录下放置urlrewrite.xml 其配置文件。重启应用即可完成安装。

3、参数介绍

(1)web.xml 下的filter 参数设置介绍

Xml代码

<filter>

<filter-name>UrlRewriteFilter</filter-name>

<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

<!-- set the amount of seconds the conf file will be checked for reload

can be a valid integer (0 denotes check every time,

-1 denotes no reload check, default -1)

设置定时检查配置文件的时间以供重新加载的时间,该参数值为整型,0为每次都检查,-1为从不检查,默认为-1

-->

<init-param>

<param-name>confReloadCheckInterval</param-name>

<param-value>60</param-value>

</init-param>

<!-- if you need to the conf file path can be changed

it is specified as a path relative to the root of your context

(default /WEB-INF/urlrewrite.xml)

设置配置文件的路径

-->

<init-param>

<param-name>confPath</param-name>

<param-value>/WEB-INF/urlrewrite.xml</param-value>

</init-param>

<!-- sets up log level (will be logged to context log)

can be: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j,

sysout:{level} (ie, sysout:DEBUG)

if you are having trouble using normal levels use sysout:DEBUG

(default WARN)

设置日志的等级

-->

<init-param>

<param-name>logLevel</param-name>

<param-value>DEBUG</param-value>

</init-param>

<!-- you can change status path so that it does not

conflict with your installed apps (note, defaults

to /rewrite-status) note, must start with /

设置状态目录,必须以/开始,默认为/rewrite-status

-->

<init-param>

<param-name>statusPath</param-name>

<param-value>/status</param-value>

</init-param>

<!-- you can disable status page if desired

can be: true, false (default true)

是否允许状态页面,默认为true

-->

<init-param>

<param-name>statusEnabled</param-name>

<param-value>true</param-value>

</init-param>

<!-- you may want to allow more hosts to look at the status page

statusEnabledOnHosts is a comma delimited list of hosts, * can

be used as a wildcard (defaults to "localhost, local, 127.0.0.1")

设置host 的列表,可以使用通配符,多个host 用逗号隔开

-->

<init-param>

<param-name>statusEnabledOnHosts</param-name>

<param-value>localhost, dev.*.myco.com, *.uat.mycom.com</param-value>

</init-param>

<!-- defaults to false. use mod_rewrite style configuration file (if this is true and confPath

is not specified confPath will be set to /WEB-INF/.htaccess) -->

<init-param>

<param-name>modRewriteConf</param-name>

<param-value>false</param-value>

</init-param>

<!-- load mod_rewrite style configuration from this parameter's value.

note, Setting this parameter will mean that all other conf parameters are ignored.

<init-param>

<param-name>modRewriteConfText</param-name>

<param-value>

RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]

RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R]

</param-value>

</init-param>

-->

<!-- defaults to false. allow conf file to be set by calling /rewrite-status/?conf=/WEB-INF/urlrewrite2.xml

designed to be used for testing only

<init-param>

<param-name>allowConfSwapViaHttp</param-name>

<param-value>false</param-value>

</init-param>

-->

</filter>

<filter-mapping>

<filter-name>UrlRewriteFilter</filter-name>

<url-pattern>/*</url-pattern>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

(2)urlrewrite.xml 配置文件参数

Xml代码

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE urlrewrite

PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"

"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite>

<rule>

<from>^/some/olddir/(.*)$</from>

<to type="redirect">/very/newdir/$1</to>

</rule>

<rule match-type="wildcard">

<from>/blog/archive/**</from>

<to type="redirect">/roller/history/$1</to>

</rule>

</urlrewrite>

配置文件规则:

urlrewirte 配置文件必须有一个urlrewrite根元素和包含至少一个rule元素 。

一个rule元素必须包含一个from 和一个to 元素,也可以包含0个以上的condition 元素和0个以上set 元素。

一个rule元素拦截用户的请求,from元素 是请求的url,to 元素是经过重写后的url 输出,下面是类似java 的重写内部实现。

Java代码

Pattern.compile(<from> element);

pattern.matcher(request url);

matcher.replaceAll(<to> element);

if ( <condition> elements match && matcher.find() ) {

handle <set> elements (if any)

execute <run> elements (if any)

perform <to> element (if any)

}

(4)元素参数说明

<urlrewrite>元素

参数取值描述
default-match-typeregex(默认)、wildcard所有的rule和condition 元素都会用到该匹配方法
decode-usingheader,utf8(默认)、null、iso-8859-1 等当url 需要解码时request.getCharacterEncoding() 将被用到,如果为空,则为utf-8
use-query-stringfalse(默认)、true语句是否加到url的后面
use-contextfalse(默认)、true上下午路径是否要加到url 中
<rule>元素

参数取值描述
enabletrue(默认)、false是否应用该rule
match-typeregex(默认)、wildcard应用那种匹配规则
实例代码:

Xml代码

<!--请求输入: /world/usa/nyc 输出为 /world.jsp -->

<!--应用java 的正则表达式-->

<rule match-type="regex">

<from>^/world/([a-z]+)/([a-z]+)$</from>

<to>/world.jsp</to>

</rule>

<!--应用wildcard表达式,该表达式后面会介绍-->

<rule match-type="wildcard">

<from>/world/*/*</from>

<to>/world.jsp</to>

</rule

<outbound-rule>元素

参数取值描述
enabledtrue(默认)、false是否应该该规则
encodefirstfalse(默认)、false是否在执行<outbound-rule>之前执行encodeURL(),ture为之后,false为之前
实例:

Xml代码

<outbound-rule>

<from>^/world.jsp?country=([a-z]+)&city=([a-z]+)$</from>

<to>/world/$1/$2</to>

</outbound-rule>

Java代码

使用jsp

<a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>

将输出

<a href="/world/usa/nyc">nyc</a>

或者使用jstl 标签

<a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a>

将输出

<a href="/world/usa/nyc">nyc</a>

<name>元素

Xml代码

<!--该规则的名称,可以用在rule元素和outbound-rule 元素中-->

lt;rule>

<name>World Rule</name>

<from>^/world/([a-z]+)/([a-z]+)$</from>

<to>/world.jsp?country=$1&city=$2</to>

</rule>

<note>元素

Xml代码

<!--用来描述该规则,可用在rule 元素和outbound-rule元素中-->

lt;rule>

<name>World Rule</name>

<note>

Cleanly redirect world requests to JSP,

a country and city must be specified.

</note>

<from>^/world/([a-z]+)/([a-z]+)$</from>

<to>/world.jsp</to>

</rule>

<condition>元素

可以用来为rule元素选择条件,所有条件将在规则执行时执行(除非显式的把“next” 设为“or”)

参数取值描述
typeheader(默认)、method、port、time等设置一些条件的类型
name可为任何值如果type 取值为header,这个名称将是http header 的值
nextand(默认)、orand:下一个rule 元素和这个rule 必须匹配。or:下一个rule元素或者这个condition 将被匹配
operatorequal(默认)、notequal、greater、less等ie请求的值和condition 值比较
实例:

Xml代码

<condition name="user-agent" operator="notequal">Mozilla/[1-4]</condition>

<condition type="user-in-role" operator="notequal">bigboss</condition>

<condition name="host" operator="notequal">www.example.com</condition>

<condition type="method" next="or">PROPFIND</condition>

<condition type="method">PUT</condition>

<from> 元素

rule 或者outbound-rule 必须至有一个from 元素,该值为url 相对于上下文的值

参数取值描述
casesensitivefalse(默认)、true是否要求该值的大小写,false为大小写匹配,true为忽略大小写
<to>元素

重写后的输出值

参数取值描述
typeforward(默认)、passthrough、redirect等url 的跳转问题
lastfalse(默认)、truefalse:余下的rule 元素将被执行,如果该规则符合的话

true:剩下的rule 元素不被执行,如果该规则符合的话

encodefalse(默认)、trueresponse.encodeURL([to]) 是否被调用。

false:将在url重写前调用。

true:将不被调用

context如果应用服务器配置允许 cross context(跨越上下文),这个属性将被forward(只有forward可以,redirct 或者其他to元素的类型都不可以)

比如在tomcat 的配置文件中设有:

<Context docBase="app" path="/app" reloadable="true" crossContext="true"/>

<Context docBase="forum" path="/forum" reloadable="true" crossContext="true"/>


<to>null</to>表示当这个规则匹配时,将不会有任何的反应。

to元素可以包含后引用(backreferences)和变量

Backreferences

比如: %N

Provides access to the grouped parts (parentheses) of the pattern from the last matched Condition in the current rule. N must be less than 10 and greater than 0 (i.e. %1, %2, %3 etc).

(上面不理解,未翻译)

变量

%{varName}

任何变量的 condition type 可以被用来作为varName。比如:%{port}将被翻译为80 ,%{year}将被翻译成2009等

函数

%{function:params}

函数可以用在set元素或者to元素中。

名称实例输入结果
replace
${replace:my cat is a blue cat:cat:dog}
my dog is a blue dog
replaceFirst
${replace:my cat is a blue cat:cat:dog}
my cat is a blue dog
escape
${escape:a b c}
a+b+c
unescape
${unescape:a+b+c}
a b c
lower
${lower:Hello World}
hello world
upper
${upper:hello}
HELLO
trim
${trim: abc def }
abc def
set元素

如果rule 匹配的话,允许你设置一些变量。

参数取值描述
typerequest、session、cookie、charset等设置域的类型
name任何数在request、session、response-header、cookie,有特殊的作用

实例:

Xml代码

<!--把client 的值设进request中,可通过request.getAttribute("client")来获取-->

<rule>

<condition name="user-agent">Mozilla/3/.0 (compatible; AvantGo .*)</from>

<from>.*</from>

<set name="client">AvantGo</set>

</rule>

<rule>

<condition name="user-agent">UP/.Browser/3.*SC03 .* </from>

<from>.*</from>

<set name="client">Samsung SCH-6100</set>

</rule>

<run> 元素

当rule和它的condition匹配时,你可以运行Object中的一个方法

参数取值描述
class你所要调用的方法的Object
methodrun(默认)你所要调用的方法,该方法必须带有(HttpServletRequest, HttpServletResponse)参数。例如:run(HttpServletRequest request, HttpServletResponse response)

如果init(ServletConfig) 或者
destroy() 方法,将会被执行,当创建或销毁该Object时
neweachtimefalse(默认)、true该Oject 是否为单例执行。false为单例,true为每次都new 新的Object
实例:

Java代码

<rule>

<from>^/world/[a-z]+/[a-z]+$</from>

<run class="com.blah.web.WorldServlet" method="doGet" />

<to>/world-presentation.jsp</to>

</rule>

lt;!--可以设置一些初始值-->

<run class="com.blah.web.MyServlet" method="doGet">

<init-param>

<param-name>someParamName</param-name>

<param-value>10</param-value>

</init-param>

</run>

才此方法中出现的异常将会被包装成ServletException 后抛出。

<class-rule>元素

每次请求都执行这个方法在rule 元素中,具体的例子在org.tuckey.web.filters.urlrewrite.sample可以查看。

参数取值描述
class运行的Object,必须带有包的全名
method

matches(默认)运行的方法,和run元素中的方法类似
lasttrue(默认)当为false时更多的rule将被执行,即使寂静匹配
实例:

Xml代码

<class-rule class="com.blah.web.MyRuleClass" />

4、小知识点

(1)xml 的一些字符必须要进行转义,如& 要转成 $amp;

(2)正则表达式里 在from 元素中,是以 ^ 开始 $结束的。例如请求:/my/url/path 将不能匹配
<from>^/url/$</from>
但能匹配
<from>/url/</from>


(3)如果你用了<outbound-rule> 元素,记得在页面输出的时候进行重写。即 jstl 表达式或者Java脚本输出

(4)如果你发现正则表达式比较难表达,可以用wildcards 表达式来写。

5、wildcard 表达式匹配方法

用wildcard 可以取代正则表达式,要使用该表达式的时候记得要在rule 元素中 把match-type 设为 wildcard ,因为默认是使用正则表达式的。

实例:

/big/url/*
匹配
/big/url/abc.html
不匹配
/big/url/abc/dir/
or
/big/url/abc/


/big/url/**
匹配
/big/url/abc.html
,
/big/url/abc/dir/
/big/url/abc/


实例:

/my/big/url/*
匹配
/my/big/url/abc.html
$1
将被设为
abc.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息