urlRewriteFileter 是apache的一个jar包,主要功能如其名:实现url的重定向。
其他功能参考其相关文档,截取部分入下:
<dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.3</version> </dependency>(2)配置方式
urlRewriteFilter本质还是一个filter,配置在Spring的web.xml里面,例如:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/url-rewrite.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如上配置表示UrlRewriteFilter 这个filter可以匹配到所有的url,具体的重定向规则见如上黄底红体字, 它表示了对UrlRewriteFilter的初始化,即对其confPath参数赋值“WEB-INF/url-rewrite.xml”, 详细可见UrlRewriteFilter的源码,参见附录。
(3)urlrewrite.xml文件
关于url-rewrite.xml文件的写法,可以参考如下示例, 尤其注意正则表达式的匹配:
#首先是文件头
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
#其次urlrewrite节点
<urlrewrite>
#再次是rule节点
<rule>
<from>/webplay3-([0-9]+)-([0-9]+).xml</from>
<to>/play/v1/getchannel/$2?enableFb=0&pre=ikan&dt=1&from=2</to>
</rule>
<rule>
<from>/(v3|1|-1)/chplay-([0-9]+)-([0-9]+)\.xml</from>
<to>/play/v1/getchannel/$3?enableFb=1&pre=client&dt=0&from=1</to>
</rule>
</urlrewrite>
具体还可参考如下博客:http://aumy2008.blogbus.com/logs/42495983.html
附:UrlRewriteFilter的源代码init方法:
public void init(FilterConfig filterConfig)
throws ServletException
{
log.debug("filter init called");
if (filterConfig == null) {
log.error("unable to init filter as filter config is null");
return;
}
log.debug("init: calling destroy just in case we are being re-inited uncleanly");
destroyActual();
this.context = filterConfig.getServletContext();
if (this.context == null) {
log.error("unable to init as servlet context is null");
return;
}
Log.setConfiguration(filterConfig);
String confReloadCheckIntervalStr = filterConfig.getInitParameter("confReloadCheckInterval");
String confPathStr = filterConfig.getInitParameter("confPath");
String statusPathConf = filterConfig.getInitParameter("statusPath");
String statusEnabledConf = filterConfig.getInitParameter("statusEnabled");
String statusEnabledOnHosts = filterConfig.getInitParameter("statusEnabledOnHosts");
String allowConfSwapViaHttpStr = filterConfig.getInitParameter("allowConfSwapViaHttp");
if (!(StringUtils.isBlank(allowConfSwapViaHttpStr))) {
this.allowConfSwapViaHttp = "true".equalsIgnoreCase(allowConfSwapViaHttpStr);
}
if (!(StringUtils.isBlank(confReloadCheckIntervalStr)))
{
this.confReloadCheckInterval = (1000 * NumberUtils.stringToInt(confReloadCheckIntervalStr));
if (this.confReloadCheckInterval < 0) {
this.confReloadCheckEnabled = false;
log.info("conf reload check disabled");
}
else if (this.confReloadCheckInterval == 0) {
this.confReloadCheckEnabled = true;
log.info("conf reload check performed each request");
}
else {
this.confReloadCheckEnabled = true;
log.info("conf reload check set to " + (this.confReloadCheckInterval / 1000) + "s");
}
}
else {
this.confReloadCheckEnabled = false;
}
String modRewriteConf = filterConfig.getInitParameter("modRewriteConf");
if (!(StringUtils.isBlank(modRewriteConf))) {
this.modRewriteStyleConf = "true".equals(StringUtils.trim(modRewriteConf).toLowerCase());
}
if (!(StringUtils.isBlank(confPathStr)))
this.confPath = StringUtils.trim(confPathStr);
else
this.confPath = ((this.modRewriteStyleConf) ? "/WEB-INF/.htaccess" : "/WEB-INF/urlrewrite.xml");
log.debug("confPath set to " + this.confPath);
if ((statusEnabledConf != null) && (!("".equals(statusEnabledConf)))) {
log.debug("statusEnabledConf set to " + statusEnabledConf);
this.statusEnabled = "true".equals(statusEnabledConf.toLowerCase());
}
if (this.statusEnabled)
{
if ((statusPathConf != null) && (!("".equals(statusPathConf)))) {
this.statusPath = statusPathConf.trim();
log.info("status display enabled, path set to " + this.statusPath);
}
}
else { log.info("status display disabled");
}
if (StringUtils.isBlank(statusEnabledOnHosts))
statusEnabledOnHosts = "localhost, local, 127.0.0.1";
else
log.debug("statusEnabledOnHosts set to " + statusEnabledOnHosts);
this.statusServerNameMatcher = new ServerNameMatcher(statusEnabledOnHosts);
String modRewriteConfText = filterConfig.getInitParameter("modRewriteConfText");
if (!(StringUtils.isBlank(modRewriteConfText))) {
ModRewriteConfLoader loader = new ModRewriteConfLoader();
Conf conf = new Conf();
loader.process(modRewriteConfText, conf);
conf.initialise();
checkConf(conf);
this.confLoadedFromFile = false;
}
else
{
loadUrlRewriter(filterConfig);
}
}