Tomcat URL Rewrite Filter isn't forwarding

2019-07-07 02:57发布

问题:

I'd like to forward all URLs matching the pattern:

http://localhost:8080/docs/view.php?fDocumentId=([0-9]+)

to:

http://localhost:8080/alfresco/service/org/docs/redirect/document/properties/$1

This is on tomcat, and there are two primary webapps being loaded: "docs" & "alfresco".
For simplicity sake, our old legacy application (LAMP based) had all URLS with /docs as the first part of the path, same as the new application. The reason I am forwarding to /alfresco is because of a webscript that I've developed to parse the old URL into a new URL and perform yet another redirect back to /docs, but this is irrelevant to this question.

The new system already has the URLRewriteFilter loaded, but was lacking the following specific code mentioned on the google code site (which I added) that needs to be put in webapps/docs/WEB-INF/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>

I then added the following rule to urlrewrite.xml in the same directory as web.xml:

<rule>
   <from>^/docs/view.php?fDocumentId=([0-9]+)</from>
   <to>/alfresco/service/org/docs/redirect/document/properties/$1</to>
</rule>

I'm getting 404's when trying to go to something like http://localhost:8080/docs/view.php?fDocumentId=12345

回答1:

Both your <from> and your <to> are incorrect.

First, your <from> needs to be relative to the current context (i.e. webapp) which is already /docs, so you want this:

<from>^/view.php?fDocumentId=([0-9]+)</from>

Second, your <to> has to be relative to the current context, too, unless you specify the context attribute and your context is defined to be cross-context. So, you'll need this:

<to context="/alfresco">/service/gov/inteldocs/redirect/document/properties/$1</to>

and you'll also need this in your META-INF/context.xml file:

<Context ... crossContext="true" ... />

Note that both of these caveats are clearly stated in the url-rewrite documentation at http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#from and http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#to (including examples for the latter).