簡體   English   中英

.htaccess 重定向不適用於 Angular 4

[英].htaccess redirects not working with 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM