我需要转发一个网址,例如

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网站上提到的特定代码,该代码需要放在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


作品。

我的错在哪里

附言:我正在使用Win10 Pro和urlrewritefilter-4.0.3

最佳答案

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


UrlRewriteFilter - Manual所述,以上内容会将/ xyz / **的请求转发到/ doc / **

出站规则类似于<rule>,但是处理传递到response.encodeURL()的URL。

09-11 20:05