Error 404 with Angular 4 using routing on Tomcat 9 with Valve

时光怂恿深爱的人放手 提交于 2020-07-20 03:58:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!