[英]AngularJS routes returns 404 error when page is being refresh
I am new to angularJS and I love coding with it. 我是angularJS的新手,我喜欢用它进行编码。 I am currently building now a web-portal using angularJS, and each of my pages uses
ngRoute
. 我目前正在使用angularJS构建一个Web门户,我的每个页面都使用
ngRoute
。 It works fine for me but when I tried to refresh the page it returns me an 404 error / page not found
. 它对我来说很好用,但是当我尝试刷新页面时,它返回
404 error / page not found
。 What should I do on this issue? 在这个问题上我该怎么办? Here's my code:
这是我的代码:
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: '/'
});
});
and in my HTML 在我的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>
And here's my .htaccess looks like 这是我的.htaccess看起来像
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 http://localhost/cvsrrc/page-not-found
Since you have used <base href="/cvsrrc/" />
and $locationProvider.html5Mode(true).hashPrefix('!');
由于您已使用
<base href="/cvsrrc/" />
和$locationProvider.html5Mode(true).hashPrefix('!');
your url removed #! 您的网址已删除#! from url and adds cvsrrc/.
从网址添加cvsrrc /。 so it will work if you redirect app from app.
因此,如果您从应用程序重定向应用程序,它将起作用。
But when you reload page it will try use url with cvsrrc/ to find path which not actually there. 但是,当您重新加载页面时,它将尝试将url与cvsrrc /一起使用,以查找实际上不存在的路径。
So you have to use rewrite module on your server to tell to redirect cvsrrc/ to #!/ 因此,您必须使用服务器上的重写模块来告诉将cvsrrc /重定向到#!/
Try either: 尝试以下任一方法:
Add below code after config: 在配置后添加以下代码:
$locationProvider.hashPrefix(""); $ locationProvider.hashPrefix(“”);
Share the url you are using to access the page. 分享您用于访问页面的网址。
Remove the ending "/" in your base. 删除基数中的结尾“ /”。
Finally I found a solution, I already met this solution before but It doesn't work for me because I forget to remove the /localhost/
from the .htaccess
! 终于我找到了一个解决方案,我之前已经遇到过这个解决方案,但是它对我不起作用,因为我忘记了从
.htaccess
删除/localhost/
! Here it is: 这里是:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.