我想转发所有与模式匹配的 URL:
http://localhost:8080/docs/view.php?fDocumentId=([0-9]+)
到:
http://localhost:8080/alfresco/service/org/docs/redirect/document/properties/$1
这是在 tomcat 上,加载了两个主要的网络应用程序:“docs”和“alfresco”。
为简单起见,我们的旧遗留应用程序(基于 LAMP)的所有 URL 都以/docs 作为路径的第一部分,与新应用程序相同。我转发到/alfresco 的原因是因为我开发了一个网络脚本来将旧 URL 解析为新 URL 并执行另一个重定向回/docs,但这与这个问题无关。
新系统已经有了URLRewriteFilter已加载,但缺少需要放入 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>
然后我将以下规则添加到与 web.xml 相同目录中的 urlrewrite.xml:
<rule>
<from>^/docs/view.php?fDocumentId=([0-9]+)</from>
<to>/alfresco/service/org/docs/redirect/document/properties/$1</to>
</rule>
当我尝试访问类似 http://localhost:8080/docs/view.php?fDocumentId=12345
最佳答案
你的<from>
和你的 <to>
不正确。
首先,您的 <from>
需要相对于已经是 /docs
的当前上下文(即 webapp) ,所以你想要这个:
<from>^/view.php?fDocumentId=([0-9]+)</from>
其次,你的<to>
也必须相对于当前上下文,除非您指定 context
属性和您的上下文被定义为跨上下文。所以,你需要这个:
<to context="/alfresco">/service/gov/inteldocs/redirect/document/properties/$1</to>
并且您的META-INF/context.xml
中也需要这个文件:
<Context ... crossContext="true" ... />
请注意,这两个注意事项在 url-rewrite 文档中都有明确说明,网址为 http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#from和 http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#to (包括后者的示例)。
关于Tomcat URL 重写过滤器不转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11438669/