Homestead .htaccess 重写规则 Angular 7

Homestead .htaccess rewrite rule with Angular 7

我尝试将 Homestead 与我的 Angular 项目一起使用,当我在浏览器中按 URI 上的刷新(实际上不存在)时,RewriteRule 没有捕捉到请求并且不会重定向到 index.html 文件,其中 Angular 可以处理 URI。

这是我的 .htaccess 文件:

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

如果在路线上刷新,我总是会收到 No input file specified. 消息。在主路径 (www.example.test/) 上,index.html 文件加载良好。

这种情况下正确的 .htaccess 配置是什么?或者,如果不是 .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>

我发现了问题。

我的 Homestead 运行 使用 Nginx http 服务器,因此 .htaccess 文件被忽略。

所以我需要编辑/etc/nginx/sites-enabled/mydomain.test文件下的配置文件,更新为:

location / {
    try_files $uri $uri/ /index.php?$query_string;

    // this is what I added:
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.html break;
    }
}

这解决了问题。