【问题标题】:AngularJS routing issue with apacheAngularJS路由问题与apache
【发布时间】:2014-06-28 16:52:24
【问题描述】:

我正在使用 Yeoman 包管理器来创建一个 Angular Web 应用程序。这是我的主控制器:

    '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 是一个客户端应用程序,所以服务器上没有任何东西可以处理这些请求。我在网上阅读了很多关于必须使用服务器重新路由或使用 apache 重写 mod,但我仍然没有让它工作。

我尝试过的事情

Apache rewrite rules not being applied for angularjs

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

我的 .htaccess 用于 apache 网络服务器:

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

【问题讨论】:

    标签: angularjs apache .htaccess mod-rewrite


    【解决方案1】:

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

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

    这个 SO 答案似乎还有很多其他有趣的链接,关于哈希/ URL 片段:https://stackoverflow.com/a/15135130

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 2014-08-21
      • 2016-11-09
      • 2015-04-20
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多