简体   繁体   English

.htaccess 重定向不适用于 Angular 4

[英].htaccess redirects not working with Angular 4

I know a lot of people ask this, but I have looked at all the answers and nothing is working.我知道很多人都会问这个问题,但是我查看了所有答案,但没有任何效果。

I am sure the problem is I need to create a .htaccess file and add it to my dist because that is what the ISP console guide says to do.我确定问题是我需要创建一个 .htaccess 文件并将其添加到我的 dist 因为这是 ISP 控制台指南所说的。

I am using Angular cli and the build command:我正在使用 Angular cli 和构建命令:

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

I have added this .htaccess file to my app folder - the same folder as my index.html file.我已将此 .htaccess 文件添加到我的应用程序文件夹 - 与我的 index.html 文件相同的文件夹。 This is the .htaccess code:这是 .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]

I tried various snippets from various different answers on SO, I tried the snippet on the angular.io guide, I tried changing the base-href.我在 SO 上尝试了各种不同答案的各种片段,我尝试了 angular.io 指南上的片段,我尝试更改 base-href。 Nothing seems to work and I can't even be sure it's being added properly to my dist in the build.似乎没有任何效果,我什至无法确定它是否已正确添加到构建中的 dist 中。 What can I do?我能做些什么?

After making a big fuss with the technical team of my hosting provider, they confirmed my .htaccess file was not being uploaded via my FTP client. 在与我的托管服务提供商的技术团队大惊小怪之后,他们确认我的.htaccess文件没有通过我的FTP客户端上传。 It looks like I should have added it to the dist folder after the build. 看起来我应该在构建之后将它添加到dist文件夹中。 The code for the .htaccess file they added is just the same as one of the versions I tried before and it works fine: 他们添加的.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>

According to the documentation https://angular-doc.ru/guide/deployment根据文档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