Angular 5路由问题 [英] Angular 5 routing issue
问题描述
我在前端应用程序中使用Angular 5,在服务器端使用Java。我在Angular路由方面遇到问题。
I am using Angular 5 for my frontend application and Java for the server side stuff. I have an issue with Angular routing.
我的问题是:
当用户输入此URL http:// example.net/public/public-data
在他的浏览器URL栏中,然后按Enter键,他得到 404
。由于此请求的资源在服务器上不可用,因此要解决此问题,我编写了 .htaccess
文件,该文件将每个请求重定向到 index.html
页。
When a user enters this URL http://example.net/public/public-data
in his browser URL bar and hits enter he gets 404
. Because of this requested resource is not available on the server so to deal with this problem I have written .htaccess
the file which redirects each and every request to the index.html
page.
此更改后, 404
问题已解决,但我面临的一个新问题
After this change, the 404
problem is solved but one new problem I am facing,
新问题是:当用户点击
http://example.net/public/public-data
他被重定向到
index.html
页面,现在我不知道他最初请求
的页面是什么。 / p>
The new problem is: When a user hits
http://example.net/public/public-data
he gets redirected toindex.html
page, now I don't know which page he has requested originally.
我怎么知道原始URL?
How can I know the original URL?
编辑
这是我的 .htaccess
文件的代码
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QSA,L]
</ifModule>
注意
我正在使用子文件夹,例如 http://example.net/public/public-data
,其中public是我的子文件夹,其中放置了Angular应用程序代码。 http://example.net
包含一些 .php
文件,这些文件与我的Angular应用程序没有任何关系。
I am using subfolders, for example, http://example.net/public/public-data
here public is my subfolder in which my Angular app code is placed. And http://example.net
contains some .php
files which do not have any connection with my Angular app.
推荐答案
删除该.htaccess代码,并使用< base href = / public / >
Remove that .htaccess code and make use of <base href="/public/">
您的.htaccess始终会检查除index.html以外的其他路径,然后重定向到该路径。
Your .htaccess will always check for the path other than index.html and redirect to it.
index.html
<head>
<base href="/public/">
<!-- Rest of your code here -->
</head>
这篇关于Angular 5路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!