我的 Tomcat 服务器正在运行两个不同的上下文,它们的 URL 如下:
myhost.url/context1/...
myhost.url/context2/...
我想将请求捕获到:
myhost.url/appname/...
其中 appname 是动态的,并将这些请求重定向到:
myhost.url/context2/appname/...
一个约束是忽略任何重写,以防请求被发送到 myhost.url/context1/... 或 ROOT 上下文下的一些静态文件。
我想使用 Tomcat 的 RewriteValve 重写 URL,因此我添加了一个具有以下配置的 ROOT 上下文:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="ROOT" reloadable="false" crossContext="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
</Context>
并在WEB-INF目录下添加了一个rewrite.config文件,配置如下:
RewriteCond %{CONTEXT_PATH} ^(?!/context1/).+$ [OR]
RewriteCond %{REQUEST_URI} ^(?!/favicon.ico) [OR]
RewriteCond %{REQUEST_URI} ^(?!/blank.html) [OR]
RewriteCond %{REQUEST_URI} ^(?!/error.html) [OR]
RewriteCond %{REQUEST_URI} ^(?!/index.html)
RewriteRule ^/(.+)$ /context2/$1 [L]
不幸的是,这不起作用,请推荐正确的重写规则。
最佳答案
我设法以满足我要求的方式执行重写表。
只有在请求的上下文不同于“context1”或“context2”并且上下文可用(成功部署)的情况下才会发生重写。 或者如果请求的 url 不同于“/”、“/favicon.ico”、“/blank.html”、“/error.html”、“/index.html”和“/images”。
RewriteCond %{CONTEXT_PATH} ^/(?!(context1|context2)) [OR]
RewriteCond %{REQUEST_URI} ^/(?=.+$)((?!(favicon.ico|blank.html|error.html|index.html|images)).*$)
RewriteRule ^(.+)$ /context2$1 [L]
关于tomcat - 将 Tomcat 中的 URL 从动态 appName 重写为现有上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888746/