18

I am trying to use the RewriteValve in Tomcat 8.0 http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html

In $CATALINA_HOME/conf/server.xml, I added

<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

right below the Host tag

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

Created a rewrite.config in the $CATALINA_HOME/conf directory with the following

RewriteRule ^/abc /myapp

where http:/host:port/myapp is a working webapp.

So now when I go to http://host:port/abc I expect to be redirected to http://host:port/myapp but I get a 404 the requested resource is not available instead.

It doesn't look like it is even reading my rewrite.config file. The access log in the logs directory just reports the "GET /abc HTTP/1.1" 404 1000 with no further details.

What am I missing? Thanks

partlycloudy
  • 419
  • 1
  • 4
  • 12
  • Have you enabled DEBUG logging for `RewriteValve`? Anything relevant in the logs? At first glance, what you have looks correct. What version of Apache Tomcat are you using? – Christopher Schultz Nov 14 '14 at 16:16
  • 2
    How can I enable DEBUG logging for `RewriteValve`? I am using Tomcat 8.0.15 – partlycloudy Nov 14 '14 at 16:22
  • Chris - Any ideas, what else can I try? I am just about at my wits end, not sure how to debug/troubleshoot this further. Thanks for any help. – partlycloudy Nov 14 '14 at 19:02
  • Read the Tomcat users' guide section on logging to see how to configure `DEBUG` logging for a single class. You basically just add a line to `logging.properites` which configures Java's logging mechanism (`java.util.log`). – Christopher Schultz Nov 17 '14 at 18:08
  • I did that already. I added a line to $CATALINA_HOME/conf/logging.properties `org.apache.catalina.valves.rewrite.RewriteValve.level = FINE` but that didn't appear to do anything, no entries in any of the log files in /logs. – partlycloudy Nov 17 '14 at 19:01
  • Chris - Any ideas? I went back to using the URLRewriteFilter at http://tuckey.org/urlrewrite/ but since this is now built into Tomcat, I would really like to use it instead of yet another third party component. – partlycloudy Nov 18 '14 at 17:07
  • 1
    Oh, unfortunately, the `RewriteValve` doesn't use its own (class-specific) logger. Instead, it logs to its container's valve. So if you have your `` configured to be a child of the ``, then you need to change the log level on the host (like this: `org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = DEBUG`). – Christopher Schultz Nov 19 '14 at 17:18
  • 7
    OK now we are getting somewhere. DEBUG didn't work. I added `org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = FINE` to the conf/logging.properties and saw `19-Nov-2014 13:30:21.322 FINE [Catalina-startStop-1] org.apache.catalina.valves.rewrite.RewriteValve.startInternal No configuration resource found: Catalina/localhost/rewrite.config in ...` in localhost.2014-11-19.log. So I moved my rewrite.config file from /conf to conf/Catalina/localhost and now it is picking up my rewrite rules. Maybe the documentation can make this clearer. Thanks. – partlycloudy Nov 19 '14 at 18:39
  • 1
    Yes, I've noticed a lot of room for improvement with the documentation. The `RewriteValve` is a new feature without a lot of mileage under its belt, so this is good feedback. – Christopher Schultz Nov 19 '14 at 19:08

4 Answers4

15

In Tomcat documentation http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html is written

The rewrite valve can be configured as a valve added in a Host. See virtual-server documentation for informations how to configure it. It will use a rewrite.config file containing the rewrite directives, it must be placed in the Host configuration folder.

so try to move rewrite.config into $CATALINA_HOME/conf/Catalina/localhost

Small hint: you can misspell something in rewrite.config ( e.g. xxRewriteRule ) and then if tomcat reads this file it fails. I think in your case tomcat starts even if you misspell something (because it is not read).

michal4
  • 151
  • 5
1

If you'd like something different than the default path $CATALINA_HOME/conf/Catalina/localhost, you might be able to make use of the resourcePath field in your RewriteValve xml definition to specify a relative path to the configuration file. (Admittedly, I have not used this.)

Alternatively, you can add the valve to $CATALINA_HOME/conf/context.xml and then move rewrite.config into your web application's WEB-INF folder.

From the documentation:

It can also be in the context.xml of a webapp. The valve will then use a rewrite.config file containing the rewrite directives, it must be placed in the WEB-INF folder of the web application

jeff
  • 70
  • 1
  • 6
  • 1
    The resourcePath field setup doesn't work. It is not a recognized property when trying to set it. Also, you can define the valve in $CATALINA_BASE/conf/[engine]/[hostname]/your_app.xml and put the rewrite.config in the WEB-INF directory of the application (your_app). – Achille May 12 '17 at 14:50
  • @Achille this does not seem to work for me either, on 8.0.53. No output from `RewriteValve` at all. – Martynas Jusevičius Mar 02 '20 at 11:28
0

I had to comment out the AccessLogValve in the server.xml for my rewrite valve to work

slugslog
  • 1,748
  • 1
  • 10
  • 10
-3

According to apacheDoc

The rewrite valve is configured as a valve using the org.apache.catalina.valves.rewrite.RewriteValve class name.

The rewrite valve can be configured as a valve added in a Host. See virtual-server documentation for informations how to configure it. It will use a rewrite.config file containing the rewrite directives, it must be placed in the Host configuration folder.

It can also be in the context.xml of a webapp. The valve will then use a rewrite.config file containing the rewrite directives, it must be placed in the WEB-INF folder of the web application