2

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.

Bopsi
  • 2,090
  • 5
  • 36
  • 58
  • 1
    maybe [this](https://stackoverflow.com/questions/46299430/deploy-angular-application-on-apache-tomcat-404-deep-links-issue) helps you – Mikefox2k Aug 02 '19 at 12:37

1 Answers1

4

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.

Bopsi
  • 2,090
  • 5
  • 36
  • 58