urlrewriter 用法
1、简介
UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。
其主页:http://tuckey.org/urlrewrite/
2、安装
在其主页下载该包文件,把其jar 包放在lib 目录下,在web.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 参数设置介绍
- <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 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 的重写内部实现。
- 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-type | regex(默认)、wildcard | 所有的rule和condition 元素都会用到该匹配方法 |
decode-using | header,utf8(默认)、null、iso-8859-1 等 | 当url 需要解码时request.getCharacterEncoding() 将被用到,如果为空,则为utf-8 |
use-query-string | false(默认)、true | 语句是否加到url的后面 |
use-context | false(默认)、true | 上下午路径是否要加到url 中 |
<rule>元素
参数 | 取值 | 描述 |
enable | true(默认)、false | 是否应用该rule |
match-type | regex(默认)、wildcard | 应用那种匹配规则 |
实例代码:
- <!--请求输入: /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>元素
参数 | 取值 | 描述 |
enabled | true(默认)、false | 是否应该该规则 |
encodefirst | false(默认)、false | 是否在执行<outbound-rule>之前执行encodeURL(),ture为之后,false为之前 |
实例:
- <outbound-rule>
- <from>^/world.jsp?country=([a-z]+)&city=([a-z]+)$</from>
- <to>/world/$1/$2</to>
- </outbound-rule>
- 使用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>元素
- <!--该规则的名称,可以用在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>元素
- <!--用来描述该规则,可用在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”)
参数 | 取值 | 描述 |
type | header(默认)、method、port、time等 | 设置一些条件的类型 |
name | 可为任何值 | 如果type 取值为header,这个名称将是http header 的值 |
next | and(默认)、or | and:下一个rule 元素和这个rule 必须匹配。or:下一个rule元素或者这个condition 将被匹配 |
operator | equal(默认)、notequal、greater、less等 | ie请求的值和condition 值比较 |
实例:
- <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 相对于上下文的值
参数 | 取值 | 描述 |
casesensitive | false(默认)、true | 是否要求该值的大小写,false为大小写匹配,true为忽略大小写 |
<to>元素
重写后的输出值
参数 | 取值 | 描述 |
type | forward(默认)、passthrough、redirect等 | url 的跳转问题 |
last | false(默认)、true |
false:余下的rule 元素将被执行,如果该规则符合的话 true:剩下的rule 元素不被执行,如果该规则符合的话 |
encode | false(默认)、true |
response.encodeURL([to]) 是否被调用。 false:将在url重写前调用。 true:将不被调用 |
context |
如果应用服务器配置允许 cross context(跨越上下文),这个属性将被forward(只有forward可以,redirct 或者其他to元素的类型都不可以) 比如在tomcat 的配置文件中设有: <Context docBase="app" path="/app" 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 匹配的话,允许你设置一些变量。
参数 | 取值 | 描述 |
type | request、session、cookie、charset等 | 设置域的类型 |
name | 任何数 |
在request、session、response-header、cookie,有特殊的作用 |
实例:
- <!--把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 | |
method | run(默认) |
你所要调用的方法,该方法必须带有(HttpServletRequest, HttpServletResponse)参数。例如:run(HttpServletRequest request, HttpServletResponse response) 如果init(ServletConfig) 或者 destroy() 方法,将会被执行,当创建或销毁该Object时
|
neweachtime | false(默认)、true | 该Oject 是否为单例执行。false为单例,true为每次都new 新的Object |
实例:
- <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元素中的方法类似 |
last | true(默认) | 当为false时更多的rule将被执行,即使寂静匹配 |
实例:
- <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
参照翻译的文档的网页是:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
水平很有限,可能翻译只能看懂一点点,请见谅!
urlrewriter 用法相关推荐
- URLRewriter在ASP.NET配置文件中的用法
<?xml version="1.0"?> <configuration> <configSections> <section ...
- urlrewriter 的用法
本博客的部分内容来自于其它博客,对原作者表示感谢. 参考文献:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/i ...
- java urlrewriter_java url重写技术(UrlReWriter)
现在url重写技术用的越来越广泛了,很高兴今天和各位网友分享一下: 首先我们要从网上下一个UrlReWriter jar包 现在地址进不了,我这里有一个3.0版本的,文章底有下载地址,大家可以去下载 ...
- URL 重写的常见用法
URL 重写的常见用法 创建数据驱动的 ASP.NET 网站时,通常会产生一个单个的网页,该网页基于查询字符串参数显示数据库数据的子集.例如,在设计电子商务站点时,您的任务之一便是允许用户浏览待售产品 ...
- c语言中external,static关键字用法
static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...
- Pandas_transform的用法
先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...
- Python中yield和yield from的用法
yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...
- pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法
squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的 ...
- python yield 和 yield from用法总结
#例1. 简单输出斐波那契數列前 N 个数 #缺点:该函数可复用性较差,因为 fab 函数返回 None,其他函数无法获得该函数生成的数列 #要提高 fab 函数的可复用性,最好不要直接打印出数列,而 ...
最新文章
- 【PHP-FPM】配置,优化性能
- 曾宝仪和机器人_你愿意和机器人成为终身伴侣吗?
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) - D Count The Bits
- Azure 内容审查器之羞羞图审查
- python实验总结心得体会_Python,Pyvisa操作Agilent 86140x系列OSA
- leetcode 1449. 数位成本和为目标值的最大数字(dp)
- Linux常用指令---netstat(网络端口)
- 学习template算法(template matching)以及改进(二)
- cemtos7.2搭建samba
- 两台计算机如何共享文档,两台电脑如何共享文件
- win11关闭开机密码的方法
- 为什么局域网 IP 通常以 192.168 开头而不是 1.2 或者 193.169 ?
- 2017下半年软件测评师上午考试试题
- 2022年ACM杰出会员名单公布:23位华人学者入选
- 点阵字体显示系列之二:汉字显示
- 4000元以内的台式计算机,4000元左右台式电脑推荐最好联想的
- TA游戏推荐:暗黑神作 《万神之怒EX》
- 普通计算器和科学计算器的实现过程另附带画图功能(C# 窗体)
- 震撼!行李自动化物流系统
- 电脑重装系统,加固态常见问题
热门文章
- Android — jimu Mirror
- XML转Json 设置指定节点为数组
- MouseManager
- PC版微信 网络不可用,请检查你的网络设置 的解决方法
- 【QTdesigner】课时37.按钮控件(QPushButton)【pyqt5+QTdesigner模式】
- java pdf工具类_Java PDF工具类(一)| 使用 itextpdf 根据PDF模板生成PDF(文字和图片)...
- multi-kernels、ALLOC与USE、Zero-Copy
- 计算机基础之计算机的发展历史
- Ubuntu 20.04.2.0 LTS 更改默认关联视频播放器VLC的方法
- SpringSecurity+Mybatis实现用户自助注册登录(含角色),打造简单安全的注册登录页面。