重写tomcat 8 URL [英] Re-writing tomcat 8 urls
问题描述
我在两个不同的上下文中部署了两个tomcat应用程序:
I have two tomcat applications deployed under two different contexts:
someurl.com/context1/
someurl.com/context2/
我需要拦截以下形式的网址:
I need to intercept urls in the form:
someurl.com/clientname/context1/
并将其重定向到url:
and redirect them to url:
someurl.com/context1/clientname
客户端名称"是动态的
我尝试在tomcats server.xml文件的元素中使用重写阀,但它仍然仅适用于包含上下文的URL.即:
I have tried using a rewrite valve in the element of my tomcats server.xml file, but it still works only for urls which include the context. i.e.:
someurl.com/context1/clientname/context1
被重写为
someurl.com/context1/clientname
使用以下正则表达式:
RewriteCond %{REQUEST_URI} ^.*/context1/.*$
RewriteRule ^.*/context1/(.*)$ /context1/$1 [L]
是否可以通过一种不考虑上下文的方式来全局重写url?
Is there a way to globally re-write urls in such a way that the context is not taken into account?
推荐答案
经过大量挖掘,我发现了一种实现所需结果的简单方法.诀窍是在没有任何实际应用程序部署的情况下建立根上下文.然后向该根上下文添加RewriteValve,如下所示:
After a lot of digging around I found out a really easy way of achieving the desired result. The trick is to set up a root context without any actual application being deployed there. Then to that root context a RewriteValve is added like this:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="ROOT" path="/" reloadable="true" crossContext="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
</Context>
将crossContext设置为true很重要,以便根上下文可以与较低级别的上下文进行通信.
It is important that the crossContext is set to true, so the root context can communicate with the lower level contexts.
然后在根上下文的WEB-INF中使用以下rewrite.config可以解决问题:
Then in WEB-INF of the root context the following rewrite.config will do the trick:
RewriteRule ^/.*/context1/(.*)$ /context1/$1 [L]
基本上意味着:捕获所有形式为 clientname/context1/etc 的ur,并将其路由到 context1/clientname/etc
which basically means: capture all ur's which have the form: clientname/context1/etc and route them to context1/clientname/etc
这篇关于重写tomcat 8 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!