[英]angularjs routing / html5mode and htaccess giving 404 error
我正在1.6x中构建我的第一个angularjs应用,我的路由器导致404错误
我的路由器看起来像这样:
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/account', {
templateUrl: 'views/account/welcome.html',
}) //works
.when('/account/emails', {
templateUrl: 'views/account/email.html',
controller: 'emailController'
}) //404 error
.when('/account/wallet', {
templateUrl: 'views/account/wallet.html',
}) //404 error
.when('/account/settings', {
templateUrl: 'views/account/settings.html',
}) //404 error
.when('/account/logout', {
templateUrl: 'views/account/logout.html',
}) //404 error
.otherwise({
redirectTo: '/'
}) //works
});
和我的htaccess看起来像这样:
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
</ifModule>
因此,我的路由器可以在该应用程序中正常工作,但是如果我的浏览器地址位于//localhost/account/emails
或类似的路径,并且刷新了页面,则会收到404错误。 当我直接从浏览器访问路径时,也会发生这种情况。
//localhost/
和//localhost/account
之所以起作用,是因为这是带有相应index.html文件的apps文件系统中的实际路径。 我在这里做错了什么?
您的.htaccess
有错误的RewriteRule
。 您要使用HTML5模式,因此您的URL将不再以/#/
作为前缀。
这可以工作:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/account.*$
RewriteRule ^(.*) /account/index.html [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.