[英]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]
重寫.config
RewriteCond %{REQUEST_URI} ^/contextOld/.*$
RewriteRule ^/contextOld/(.*)$ /context-new/$1 [L]
服務器.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 "%r" %s %b [%D]" />
<Valve className="org.apache.catalina.valves.ErrorReportValve" errorCode.400="/webapps/Error/error.html" showServerInfo="false"/>
</Host>
如果我在這里遺漏了任何其他相關的 tomcat 配置,我可以提供。
如果/contextOld
不對應於任何應用程序(您既沒有ROOT
應用程序也沒有/contextOld
應用程序),您可能偶然發現了錯誤 64593 。
這已在Tomcat 9.0.38中得到糾正,因此您只需升級到最新版本即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.