我是angularJS的新手,我喜欢用它编码。我目前正在使用angularJS构建一个Web门户,我的每个页面都使用ngRoute
。它适用于我,但当我尝试刷新页面时,它返回404 error / page not found
。我该怎么办?这是我的代码:
var app = angular.module("CVSRRC", ['ngMaterial','ngRoute']);
app.controller('CVSRRC-CTRL', function($scope, $http, ...){
// some codes here...
});
app.config(function($routeProvider, $locationProvider){
$locationProvider.html5Mode(true).hashPrefix('!');
$routeProvider
.when('/', {
templateUrl: '_pages/home',
controller: 'homeCTRL',
resolve: {
delay: function($q, $timeout){
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
})
...etc...
.otherwise({
redirectTo: '/'
});
});
并在我的 HTML
中<html>
<head>
<base href="/cvsrrc/" />
</head>
<body ng-app="CVSRRC" ng-controller="CVSRRC-CTRL">
<div class="row" id="main">
<div ng-view ng-show="statechange"></div>
<div ng-show="!statechange" cvsrrc-resolve>
<div id="_loader_"></div>
</div>
</div>
<a href="about-us">About</a>
<a href="login">login</a>
//etc
</body>
</html>
这是我的.htaccess看起来像
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 http://localhost/cvsrrc/page-not-found
答案 0 :(得分:1)
由于您已使用<base href="/cvsrrc/" />
和$locationProvider.html5Mode(true).hashPrefix('!');
您的网址已删除#!从url并添加cvsrrc /。因此,如果您从应用程序重定向应用程序,它将起作用。
但是当你重新加载页面时,它会尝试使用带有cvsrrc /的url来查找实际上并不存在的路径。
所以你必须在你的服务器上使用重写模块告诉你将cvsrrc /重定向到#!/
答案 1 :(得分:0)
尝试:
在config:
之后添加以下代码$ locationProvider.hashPrefix( “”);
分享您用来访问该页面的网址。
删除基座中的结尾“/”。
答案 2 :(得分:0)
最后我找到了一个解决方案,我之前已经遇到过这个解决方案,但它对我不起作用,因为我忘了从/localhost/
删除.htaccess
!这是:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>