Tomcat URL重写筛选器工作不正常

时间:2015-11-25 09:15:43

标签: java tomcat url-rewriting

我需要转发一个像

这样的网址
http://localhost:8080/doc/service?type=testLogin&exectype=userShow&moreparms

为:

http://localhost:8080/xyz/service?type=testLogin&exectype=userShow&moreparms

系统使用tomcat 8.0.28运行并且已经加载了URLRewriteFilter(http://www.tuckey.org/urlrewrite/),但是没有使用我在googlecode.com上的urlrewritefilter-site上提到的以下特定代码需要放入webapps / doc / WEB-INF / web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

    <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>DEBUG</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

我将以下规则添加到与web.xml相同的目录中的urlrewrite.xml:

<urlrewrite>
    <rule>
       <from>^/doc/(.*)$</from>
       <to type="redirect">/xyz/$1</to>
    </rule>

    <outbound-rule>
    </outbound-rule>
<urlrewrite>

在尝试去

之类的时候,我得到了404
http://localhost:8080/doc/service?type=testLogin&exectype=userShow&moreparms

如果我尝试

,我也会收到404错误
http://localhost:8080/doc/status

使用示例规则:

<urlrewrite>
    <rule>
        <from>/doc/status/</from>
        <to type="redirect">%{context-path}/rewrite-status</to>
    </rule>
    <outbound-rule>
       <from>/rewrite-status</from>
       <to>/doc/status/</to>
    </outbound-rule>
<urlrewrite>

但是

http://localhost:8080/doc/rewrite-status

作品。

我的错在哪里?

P.S:我正在使用Win10 pro和urlrewritefilter-4.0.3

1 个答案:

答案 0 :(得分:0)

<rule>
   <from>^/doc/(.*)$</from>
   <to type="redirect">/xyz/$1</to>
</rule>

应该交换规则中的<from><to>。尝试类似:

<rule match-type="wildcard">
   <from>^/xyz/**</from>
   <to>/doc/$1</to>
</rule>

以上内容会将/ xyz / **的请求转发给UrlRewriteFilter - Manual

中所述的/ doc / **

出站规则与<rule>类似,但处理传递给response.encodeURL()的网址。