Home > Software engineering >  Tomcat rewrite url gives 404 while direct browsing gives 200
Tomcat rewrite url gives 404 while direct browsing gives 200

Time:10-01

I think I have done some searching before posting this. Couldn't find a similar problem. I am using apache-tomcat-9.0.37. I am trying to forward the requests from /contextOld/ to /context-new/.

It is successfully rewriting to new url. But when the tomcat rewrites to the new url, it is giving a 404. When accessed directly via browser gives a 200.

localhost log

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/(.*)$

The second request in the below accesss log is when accessing the url directly via a browser

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/$1 [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>

I can provide any other relevant tomcat configuration if I have missed any here.

CodePudding user response:

If /contextOld does not correspond to any application (you don't have neither a ROOT application nor a /contextOld application), you might have stumbled upon bug 64593.

This was corrected in Tomcat 9.0.38, so you just have to upgrade to the newest version.

  • Related