在带有Valve的Tomcat 9上使用路由的Angular 4错误404 [英] Error 404 with Angular 4 using routing on Tomcat 9 with Valve

查看:126
本文介绍了在带有Valve的Tomcat 9上使用路由的Angular 4错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通过以下步骤告诉您会发生什么情况:

I'll tell you what happens by following steps:

1)我有基于Angular 4.0.2的应用程序项目.我正在使用路由.这些是我在app.module.ts中的路线:

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 }
];

因此,Web应用程序的第一个屏幕是登录".

So the first screen of the web application is Login.

2)我执行了ng build -prod

2) I executed the command ng build -prod

3)我将文件夹dist移到Tomcat webapps文件夹中

3) I move the folder dist into Tomcat webapps folder

4)我修改了文件index.html,更改了基本href.现在是

4) I modified the file index.html changing base href. Now it's

5)在/Tomcat9/conf/context.xml中,我添加了以下指令:

5) In /Tomcat9/conf/context.xml, I added the following instruction:

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

6)我创建了文件夹WEB-INF,然后创建了文件rewrite.config,该文件包含: #如果请求了现有资产或目录,请直接按原样

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)我启动了Tomcat服务器

7) I started Tomcat server

8)接下来,我打开了Mozilla浏览器,并放入了地址栏 本地主机:8080/dist/ 出现登录屏幕,其中包含2个文本框(用户和密码)和一个用于登录的按钮.

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)当我与用户按下按钮并通过时才有效.它转到仪表板屏幕,我看到地址栏变为: 本地主机:8080/dist/仪表板.

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)之后,我转到地址栏,然后手动编写:localhost:8080/dist/dashboard.

10) After that, I go to address bar and I wrote manually: localhost:8080/dist/dashboard.

然后按Enter键,出现错误404.

Then I press Enter and it appears Error 404.

它应该一直显示仪表板屏幕,而不是错误404.

It should keep showing dashboard screen and not Error 404.

此应用程序在具有.htaccess的Apache服务器中运行良好,但在Tomcat 9中却无法运行

This app works well in Apache server with .htaccess, but not in Tomcat 9

如果配置了rewrite.config,为什么在这种情况下不显示仪表板屏幕?为了避免错误404,缺少了什么?

Why is not showing dashboard screen in this case if I configured the rewrite.config? what is missing in order to avoid error 404?

推荐答案

似乎重写规则不起作用. 尝试以下规则:

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

这篇关于在带有Valve的Tomcat 9上使用路由的Angular 4错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆