2

I have a requirement to allow certain URLs without a context to be forwarded to specific webapp.

So, I have a webapp "app1" deployed and working. The following URL http://localhost:8080/app1/ping is working fine. In $CATALINA_HOME/webapps I only have app1.war, no ROOT.war

I want to configure the rewrite valve to redirect http://localhost:8080/ping to /app1/ping

In my server.xml I have enabled the Rewrite valve. Then I created $CATALINA_HOME/conf/Catalina/localhost/rewrite.config with the following content:

RewriteRule ^/ping /app1/ping

In the Tomcat logfile I can see the rule is invoked:

25-Nov-2019 09:02:11.442 FINE [http-nio-8080-exec-1] org.apache.catalina.valves.rewrite.RewriteValve.invoke Rewrote /ping as /app1/ping with rule pattern ^/ping

But nevertheless I get a 404 in the browser for /ping (but not for /app1/ping)

In the access log I can see both request show up with the same URL, but the redirected one (the first line in the below log) is listed with a 404

0:0:0:0:0:0:0:1 [25/Nov/2019:09:02:11 +0100] "GET /app1/ping HTTP/1.1" 404 1078
0:0:0:0:0:0:0:1 [25/Nov/2019:09:02:23 +0100] "GET /app1/ping HTTP/1.1" 200 4

What am I missing here?

I am using Tomcat 8.5.40 together with Oracle's JDK 8 on Windows 10

If I add a redirect flag to the rule:

RewriteRule ^/ping /app1/ping [R]

then I get a NPE inside Tomcat:

java.lang.NullPointerException
  at org.apache.catalina.connector.Response.sendRedirect(Response.java:1318)
  at org.apache.catalina.connector.Response.sendRedirect(Response.java:1288)
  at org.apache.catalina.valves.rewrite.RewriteValve.invoke(RewriteValve.java:435)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
  at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
  at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1506)
  at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:748)
  • Did you check https://stackoverflow.com/questions/34619751/tomcat-8-url-rewrite-issues – Ori Marko Nov 25 '19 at 08:13
  • @user7294900: that is about redirecting inside a single webapp. I am essentially trying to do that _before_ my webapp is accessed, adding a context to a URL without a context (without using a ROOT.war). I am also not enabling the valve for a specific context (because it should work _without_ one) –  Nov 25 '19 at 08:22
  • I'm having the same problem. Strange thing is that I had simulated the very same thing you're doing on my Windows development machine using Tomcat 8.5.58 and it worked, but Ubuntu Server's Tomcat 8.5.39-1ubuntu1~18.04.3 has the problem you're seeing. So it might be a bug in Tomcat? – Alexander Malfait Oct 05 '20 at 11:07

1 Answers1

2

The problem here is that there is no context bound to the request. Make sure to configure some (empty) ROOT context that the request gets bound to.

Sandworm
  • 41
  • 3