问题
I'll tell you what happens by following steps:
1) I have my app project based on Angular 4.0.2. I'm using routing. These are my routes in app.module.ts:
const routes: Routes =
[
{ path: '', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'login', component: LoginComponent }
];
So the first screen of the web application is Login.
2) I executed the command ng build -prod
3) I move the folder dist into Tomcat webapps folder
4) I modified the file index.html changing base href. Now it's
5) In /Tomcat9/conf/context.xml, I added the following instruction:
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
6) I created the folder WEB-INF, then I created the file rewrite.config, this file contains: # 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 ^ /index.html
7) I started Tomcat server
8) Next I openned a Mozilla browser and I put in address bar localhost:8080/dist/ It appears the login screen with 2 textboxes (user and pass) and one button for login.
9)When I press the button with user and pass valid. it goes to dashboard screen and I see the address bar changes to: localhost:8080/dist/dashboard.
10) After that, I go to address bar and I wrote manually: localhost:8080/dist/dashboard.
Then I press Enter and it appears Error 404.
It should keep showing dashboard screen and not Error 404.
This app works well in Apache server with .htaccess, but not in Tomcat 9
Why is not showing dashboard screen in this case if I configured the rewrite.config? what is missing in order to avoid error 404?
回答1:
It seems that the rewrite rules are not working. Try these rules:
#Check if the uri ends with an file extension
RewriteCond %{REQUEST_URI} .*\.(.*)$ [OR]
#Check if the uri contains /api/
RewriteCond %{REQUEST_URI} ^(.*)(/api/).*$ [OR]
RewriteRule ^(.*)$ - [L]
#If the requested resource doesn't exist, use index.html
RewriteRule ^(.*)$ /index.html
来源:https://stackoverflow.com/questions/43509519/error-404-with-angular-4-using-routing-on-tomcat-9-with-valve