【问题标题】:Angular 5 routing issueAngular 5路由问题
【发布时间】:2018-03-17 17:50:06
【问题描述】:

我在前端应用程序中使用 Angular 5,在服务器端使用 Java。我对 Angular 路由有疑问。

我的问题是:

当用户在他的浏览器 URL 栏中输入此 URL http://example.net/public/public-data 并按 Enter 键时,他会得到 404。由于这个请求的资源在服务器上不可用,所以为了解决这个问题,我编写了.htaccess 将每个请求重定向到index.html 页面的文件。

在这个改变之后,404 的问题解决了,但是我面临一个新的问题,

新的问题是:当用户点击 http://example.net/public/public-data 他被重定向到 index.html页面,现在不知道他请求的是哪个页面 原来。

我怎样才能知道原始网址?

编辑

这是我的.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 包含一些与我的 Angular 应用程序没有任何联系的 .php 文件。

【问题讨论】:

  • 你能分享你的 .htaccess 文件吗?或者,您可以使用 中的 标记来设置基本路径
  • @Neeraj 我已经更新了我的代码。

标签: javascript routing angular5


【解决方案1】:

删除该 .htaccess 代码并使用 &lt;base href="/public/"&gt;

您的 .htaccess 将始终检查 index.html 以外的路径并重定向到它。

index.html

<head>
<base href="/public/">
<!-- Rest of your code here -->
</head>

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2018-09-23
    • 2023-03-06
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    相关资源
    最近更新 更多