【问题标题】:Rewrite URLs in Tomcat from dynamic appName to existing context将 Tomcat 中的 URL 从动态 appName 重写为现有上下文
【发布时间】: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


    【解决方案1】:

    我设法以满足我要求的方式执行重写表。

    仅当请求的上下文不同于“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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2015-05-08
      • 2014-07-16
      • 2017-08-12
      • 2021-10-11
      相关资源
      最近更新 更多