2014-06-28 80 views
0

我使用Yeoman包管理器来创建一个角度的web应用程序。这是我的主控制器:apache的AngularJS路由问题

'use strict'; 

angular 
    .module('resFourApp', [ 
    'ngCookies', 
    'ngResource', 
    'ngSanitize', 
    'ngRoute', 


    ]) 
    .config(function ($routeProvider, $locationProvider) { 
    $routeProvider 
     .when('/', { 
     templateUrl: 'views/main.html', 
     controller: 'MainCtrl' 
     }) 

     .when('/about', { 
     templateUrl: 'views/about.html', 
     controller: 'AboutCtrl' 
     }) 

     .when('/contact', { 
     templateUrl: 'views/contact.html', 
     controller: 'ContactCtrl' 
     }) 
     .otherwise({ redirectTo: '/' }); 

     // use the HTML5 History API 
    // use the HTML5 History API 
    $locationProvider.html5Mode(true); 
    }); 

如果我从基本目录访问应用程序,但如果我从URL访问它,去www.base.com/route我得到一个404错误,工作正常。我理解的是因为Angular是一个客户端应用程序,所以服务器上没有任何东西可以处理这些请求。我已经阅读了很多关于必须重新路由服务器或mod用apache重写的内容,但是我仍然没有得到它的工作。

事情我已经试过

Apache rewrite rules not being applied for angularjs

https://groups.google.com/forum/#!msg/angular/GmNiNVyvonk/mmffPbIcnRoJ

我的Apache Web服务器的.htaccess:

# BEGIN angularJS 
<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)  /index.html/#/$1 
</IfModule> 
# END angularJS 

回答

2

我已经尝试了一些事情重写和不能得到它的工作,但找到了一个可行的解决方案,使用重定向和NE flag

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$  #/$1 [NC,R,NE] 

似乎有不少其他有趣的链接来自这对于哈希/ URL片段SO回答:https://stackoverflow.com/a/15135130