Angular 5 routing issue

倾然丶 夕夏残阳落幕 提交于 2020-01-06 07:26:16

问题


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 to index.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

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