问题
I am using Angular 5 for my frontend application and Java for the server side stuff. I have an issue with Angular routing.
My problem is:
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.
After this change, the 404
problem is solved but one new problem I am facing,
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.
How can I know the original URL?
EDIT
Here is the code of my .htaccess
file
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QSA,L]
</ifModule>
NOTE
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.
回答1:
Remove that .htaccess code and make use of <base href="/public/">
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>
来源:https://stackoverflow.com/questions/49332924/angular-5-routing-issue