Tomcat 重写 url 给出 404 而直接浏览给出 200

Tomcat rewrite url gives 404 while direct browsing gives 200

我想我在发布之前做了一些搜索。找不到类似的问题。我正在使用 apache-tomcat-9.0.37。我正在尝试将请求从 /contextOld/ 转发到 /context-new/。

正在成功改写为新 url。但是当 tomcat 重写为新的 url 时,它会给出 404。当直接通过浏览器访问时会给出 200。

本地主机日志

30-Sep-2021 14:01:27.250 FINE [http-nio-8080-exec-8] org.apache.catalina.core.ApplicationDispatcher.doForward  The Response is vehiculed using a wrapper: org.springframework.security.web.firewall.FirewalledResponse
30-Sep-2021 14:02:58.978 FINE [http-nio-8080-exec-3] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:04:31.042 FINE [http-nio-8080-exec-1] org.apache.catalina.core.ApplicationDispatcher.doForward  Disabling the response for further output
30-Sep-2021 14:04:31.042 FINE [http-nio-8080-exec-1] org.apache.catalina.core.ApplicationDispatcher.doForward  The Response is vehiculed using a wrapper: org.springframework.security.web.firewall.FirewalledResponse
30-Sep-2021 14:05:52.290 FINE [http-nio-8080-exec-1] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:11:01.225 FINE [http-nio-8080-exec-8] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:11:47.901 FINE [http-nio-8080-exec-6] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:12:23.485 FINE [http-nio-8080-exec-8] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:12:24.752 FINE [http-nio-8080-exec-6] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$
30-Sep-2021 14:41:04.800 FINE [http-nio-8080-exec-2] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /contextOld/mainPage.htm as /context-new/mainPage.htm with rule pattern ^/contextOld/(.*)$

下面访问日志中的第二个请求是通过浏览器直接访问url

localhost_access.log

0:0:0:0:0:0:0:1 - - [30/Sep/2021:14:41:04 +0000] "GET /context-new/mainPage.htm HTTP/1.1" 404 653 [2]
0:0:0:0:0:0:0:1 - - [30/Sep/2021:14:41:57 +0000] "GET /context-new/mainPage.htm HTTP/1.1" 200 9401 [69]

rewrite.config

RewriteCond %{REQUEST_URI} ^/contextOld/.*$
RewriteRule ^/contextOld/(.*)$ /context-new/ [L]

server.xml

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">     
        <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b [%D]" />
               
        <Valve className="org.apache.catalina.valves.ErrorReportValve" errorCode.400="/webapps/Error/error.html" showServerInfo="false"/>

      </Host>

如果我在这里遗漏了任何其他相关tomcat配置,我可以提供。

如果 /contextOld 不对应任何应用程序(您既没有 ROOT 应用程序也没有 /contextOld 应用程序),您可能偶然发现了 bug 64593.

这已在 Tomcat 9.0.38 中更正,因此您只需升级到最新版本即可。