I'm trying to figure out the way how to setup Apache Tomcat server to serve angular application with deep links. For example:
A static server routinely returns index.html when it receives a request for mysite.com/. But it rejects mysite.com/heroes/42 and returns a 404 - Not Found error unless it is configured to return index.html instead.
I want to serve angular app on localhost/angular, I have tried following:
1) Build angular application with:
ng build --prod --base-href .
2) Copy contents of build folder(default: dist) to $ApacheLocation/webapps/angular
3) Add RewriteRules at $ApacheLocation/conf/Catalina/localhost/rewrite.config
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^\/angular\/.*$ /angular/index.html
4) Adding valve just below Host tag
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
5) Starting Tomcat server and going to localhost/angular will give me:
Uncaught SyntaxError: Unexpected token < for all .js bundles (e.g main.bundle.js)
If I don't include rewrite rules, tomcat will serve localhost/angular as expected but will give 404 on deep links.
My setup configuration:
- tomcat version 9.0.0.M26
- angular version 4.4.2
- @angular/cli: 1.4.2
- node: 8.5.0
- npm: 5.4.2
- os: linux x64
ng build --prod --base-href).