【问题标题】:Angular application cannot find files in sub directoryAngular 应用程序在子目录中找不到文件
【发布时间】:2019-11-10 11:04:27
【问题描述】:

我想在 Apache 子目录中部署 Angular 应用程序。我添加了这个 .htaccess 配置:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /admin/
  RewriteCond %{REQUEST_FILENAME} -s [OR]
  RewriteCond %{REQUEST_FILENAME} -l [OR]
  RewriteCond %{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^.*$ - [NC,L]
  RewriteRule ^(.*) index.html [NC,L]
</IfModule>

我使用这个命令编译代码:

ng build --prod --base-href /admin/

但是当我打开一个页面时,我得到了这个错误:

Error: Uncaught (in promise): e: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":404,"statusText":"Not Found","url":"http://185.185.185.185/admin/api/transaction_notes/unique_id/219","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://185.185.185.185/admin/api/transaction_notes/unique_id/219: 404 Not Found","error":"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL /admin/api/transaction_notes/unique_id/219 was not found on this server.</p>\n<hr>\n<address>Apache/2.4.18 (Ubuntu) Server at 185.185.126.15 Port 80</address>\n</body></html>\n"}

你能给我一些建议,我该如何解决这个问题?

【问题讨论】:

  • 这是文件:pastebin.com/zYqdPVgs
  • 哪一行,你能粘贴整个配置作为答案吗?
  • 我测试了它,但它不起作用。
  • 我会逐行删除,看看是哪一行导致了问题

标签: angular typescript apache mod-rewrite


【解决方案1】:

您的错误看起来与使用相对 URL 的 API 调用有关,但与应用加载本身无关。如果这是唯一的问题,您可以通过在您的 url 前面添加一个斜杠以简单的方式解决它:

this.http.get('/api/transaction_notes')

而不是this.http.get('api/transaction_notes')

对于其他任何事情,您可能需要提供更多信息和代码。

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2019-05-19
    • 2017-11-28
    • 2016-07-23
    • 2017-08-02
    • 1970-01-01
    • 2017-11-22
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多