Tomcat URL Rewrite Filter isn't forwarding

烂漫一生 提交于 2019-12-10 19:42:45

问题


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



来源:https://stackoverflow.com/questions/11438669/tomcat-url-rewrite-filter-isnt-forwarding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!