【发布时间】:2017-03-19 15:59:31
【问题描述】:
我的 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]
很遗憾,这不起作用,请推荐正确的重写规则。
【问题讨论】:
标签: tomcat url-rewriting