16

I have an AngularJS webapp and Jersey backend. I need to setup URL rewriting, so everything except given exceptions will be rewritten to Angular's index.html.

Eg.:

http://my.domain.com/about will be rewritten
http://my.domain.com/photos/photo1.jpg will NOT be rewritten (file photo 1 exists)
http://my.domain.com/rest/myservice will be NOT be rewritten (it is a call to REST service)

I have set up the Tomcat 8 URL Rewrite Valve as follows:

in conf/server.xml

<Host name="my.domain.com" appBase="webapps/MyDomainServer" unpackWARs="true"
           autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
  <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
  <!-- access logging, aliases,...-->
</Host>

in conf/Catalina/my.domain.com/rewrite.config

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} ^/rest.*
RewriteRule ^ - [L]

RewriteRule ^ index.html [L]

Tomcat ignores my rewrite settings, nothing is rewritten, no error/exception is in the log. What am I doing wrong? Thanks in advance.

I have tried to move RewriteValve to config.xml in META-INF and rewrite config to WEB-INF, but it behaved in the same way.

kamelot
  • 491
  • 1
  • 7
  • 19
  • Can you'll provide solution for https://stackoverflow.com/questions/66472682/redirect-sub-domain-to-specific-mapping – aditya desai Mar 09 '21 at 08:03

3 Answers3

13

I have found the solution, problem was in wrong/faulty rewrite.config file. Correct should be:

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

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

On the first line are enumerated URIs which should not be rewritten. Everything else will be rewritten to index.html.

kamelot
  • 491
  • 1
  • 7
  • 19
  • 1
    But why was it faulty? i have the same issue. %{REQUEST_FILENAME} is always NULL... – Mario Eis Oct 08 '15 at 11:15
  • 1
    ... and SCRIPT_FILENAME is correct, but RewriteCond %{SCRIPT_FILENAME} -f complains about a missing leading / in the resource. Its a Windows system. So the path is C:\... without leading / – Mario Eis Oct 08 '15 at 12:05
  • In our case, we used {REQUEST_URI} instead of (REQUEST_FILENAME}. We couldn't drop the rules completely because we were unable to load assets local to the application, but changing the !-f and !-d to use REQUEST_URI seems to be working, at least for now. – Dave DuPlantis Jan 17 '18 at 22:03
5

Is this deployed as a java web app (WAR)? You could implement this in your web.xml:

<servlet>
   <servlet-name>index</servlet-name>
   <jsp-file>/index.html</jsp-file>
</servlet>

<servlet-mapping>
   <servlet-name>index</servlet-name>
   <url-pattern>/</url-pattern>
   <url-pattern>/about</url-pattern>
   .. as many as you need ..
<servlet-mapping>
Nicholas Hirras
  • 2,592
  • 2
  • 21
  • 28
  • Thanks Nicholas, this seems to be an interesting option, but I have managed to solve it using rewrite. I had faulty rewrite.config file. – kamelot Mar 04 '15 at 21:23
  • This is absolutely the wrong solution. The accepted answer is dynamic, where this answer is hardcoded – Talador12 Aug 10 '18 at 21:43
  • 2
    @Talador12, for certain circumstances, this works well and is appropriate. Consider a Tomcat instance serving multiple war files. The dynamic solution must be addressed to accommodate anything new and different. This solution is self contained. – J E Carter II Jan 07 '19 at 14:41
3

I couldn't get this to work with the REQUEST_URI, and I didn't like having to whitelist specific files anyway, so I solved it in a slightly different manner.

mhvelplund
  • 2,099
  • 3
  • 22
  • 38