AngularJS - 使用$ locationProvider删除哈希#

时间:2016-03-11 08:02:04

标签: javascript angularjs .htaccess

我使用$locationProvider从此hashtag #

中删除url
http://localhost/angular-phonecat/app/#/phones

使用以下代码:

phonecatApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

  $routeProvider.
    when('/phones', {templateUrl: 'partials/phone-list.html', controller: 'PhoneListCtrl'}).
    when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: 'PhoneDetailCtrl'}).
    otherwise({redirectTo: '/phones'});

  $locationProvider.html5Mode(true);

}]);

并在index.html中添加了这个

<base href="/angular-phonecat/app/">

这有效,我现在的网址是:

http://localhost/angular-phonecat/app/phones

但是当我直接访问此url时,它没有显示网页,因为它没有调用index.html文件。相反,它显示/app/phones目录

的内容

enter image description here

我尝试创建.htaccess文件

RewriteEngine   On
RewriteBase     /
RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ./index.html [L]

1 个答案:

答案 0 :(得分:1)

规则中存在一个小错误,重写规则需要多一个参数。

此外,当您编写RewriteCond %{REQUEST_FILENAME} !-d时,如果存在具有此名称的目录,则会阻止重定向。由于存在/anular-phonecat/app/phones,您还需要删除此行。

您不需要RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)条件,因为RewriteCond %{REQUEST_FILENAME} !-f条件无法重定向文件

结果是:

RewriteEngine   On
RewriteBase     /angular-phonecat/app/ 
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteRule     .* ./index.html [L]

根据评论编辑的回复

相关问题