1

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?

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
Dan
  • 115
  • 1
  • 10

1 Answers1

1

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.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Thanks for the answer. I don't fully get the problem that you mention. My css and js files are present as such: `/myapp/project/css/project-123.css` `/myapp/project/js/project-123.js` However, these are showing index.html content. – Dan Jan 06 '17 at 17:11
  • Neither of those filenames match your regular expression pattern: they start with `/myapp` and not `/css`. – Christopher Schultz Jan 06 '17 at 19:35
  • Thanks for identifying the issue @Christopher. I changed my RegEx to `(?!.*\.(?:jpg|png|css|js|json|scss)$).*$` and now it works. – Dan Jan 07 '17 at 02:37
  • I am facing a different issue now where my javascript (jQuery api, and other javascript files) are getting loaded twice. Do you know if I am missing something in the rewrite or any other potential cause? I have also asked this as a separate question here: [link](http://stackoverflow.com/questions/41528846/javascript-being-loaded-twice-after-enabling-rewrite-through-tomcat-valve) – Dan Jan 08 '17 at 02:33