Search code examples
angularangular-routingfallbacktomcat9

How to configure fallback url in apache tomcat 9


I am trying to deply angular 8 app with routing in apache tomcat 9.

I have use ng build --prod --base-href=/ng-app/ to build distributables.

I am placing ng-app folder in webapps folder of tomcats. It renders localhost:8080/ng-app properly but whenever I try to access localhost:8080/ng-app/users it's giving me 404.

I understand from here that I need to configure fallback for angular in the app server, so that it redirects to index.html if any thing under ng-app/ is not found.

I came across configuration for apache http server, ngnix, etc. But I couldn't find same for apache tomcat 9.


Solution

  • Found a solution

    First I had to add (not replace) below line under HOST

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

    I think the above configuration is for tomcat to read rewrite config which I am about to mention below -

    and create file rewrite.config in /conf/Catalina/localhost/ directory and add below rule -

    RewriteCond %{REQUEST_PATH} !-f
    RewriteRule ^/ng-app/(.*) /ng-app/index.html
    

    This rule basically redirects to /ng-app/index.html if anything below /ng-app/ is not found. Now it's working as expected.