Search code examples
tomcatmod-rewritetomcat-valve

Tomcat valve rewrite not working


I want to direct any link other than a static resource (e.g. js, images, etc.) to my index.html. I have made the following changes.

I added a context.xml in app.war/META-INF directory that contains:

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

I also have rewrite.config in app.war/WEB-INF directory that contains:

RewriteCond %{REQUEST_URI} ^/(css|img|js|partials|rest|favicon).*$
RewriteRule ^.*$ - [L] 
RewriteRule ^.*$ /index.html [L,QSA]

However, with the above settings, what happens is that all of my js and css files show index.html contents (in browser console). Could you please suggest what would be the issue here?


Solution

  • Your final rule re-writes everything to /index.html:

    RewriteRule ^.*$ /index.html [L,QSA]
    

    If you think the above rules will match first, you are right, except that a request to a file e.g. /styles.css won't match the expression you have for the static artifacts:

    RewriteCond %{REQUEST_URI} ^/(css|img|js|partials|rest|favicon).*$
    

    The problem is that you are requiring the css portion of the request URI to have at least one character after it. A request to /css/style.css should work.