.htaccess 重定向不适用于 Angular 4



我知道很多人问这个问题,但我看了所有的答案,没有任何效果。

我确定问题是我需要创建一个 .htaccess 文件并将其添加到我的 dist 中,因为这就是 ISP 控制台指南所说的。

我正在使用 Angular cli 和构建命令:

ng build --aot --prod --base-href ./

我已将此.htaccess文件添加到我的应用程序文件夹中 - 与我的index.html文件相同的文件夹。这是 .htaccess 代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]

我在 SO 上尝试了来自各种不同答案的各种片段,我尝试了 angular.io 指南上的片段,我尝试更改 base-href。似乎没有任何效果,我什至无法确定它是否已正确添加到构建中的我的dist中。我能做什么?

在与我的托管服务提供商的技术团队大惊小怪后,他们确认我的 .htaccess 文件不是通过我的 FTP 客户端上传的。看起来我应该在构建后将其添加到 dist 文件夹中。他们添加的.htaccess文件的代码与我之前尝试过的版本之一相同,并且工作正常:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

根据文档 https://angular-doc.ru/guide/deployment

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

最新更新