user330315
user330315

Reputation:

Tomcat 8.5 rewrite Valve to redirect root request to specific webapp

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)

Upvotes: 2

Views: 2432

Answers (1)

Sandworm
Sandworm

Reputation: 41

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.

Upvotes: 2

Related Questions