首页 > 解决方案 > 使用 htaccess 重写 URL 得到 404 错误 - AngularJS html5 模式

问题描述

删除 # 和后 Angular js 页面刷新 404 错误!来自网址

我正在处理一个 angular(1.7.5) 项目,我想在其中删除 # 和 ! 从 URL,为此,我使用以下代码

myApp.config(['$locationProvider', function ($locationProvider) {
     $locationProvider.html5Mode(true);
}]);

并在根文件夹的 index.html 中添加了基本标记

<base href="/myApp/">

和 .htaccess 文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

最后,我删除了 # 和 ! 来自 URL,但问题是当我刷新页面或在新选项卡中打开相同的 URL 时,页面出现 404 错误,

http://localhost/dev/ ----- works fine

http://localhost/dev/login ----404 error 

我不知道如何解决问题帮助我谢谢。

标签: javascripthtmlangularjsapache.htaccess

解决方案


尝试.htaccess像这样修改规则:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]

推荐阅读