AngularJS路由问题与Apache [英] AngularJS routing issue with apache

查看:1021
本文介绍了AngularJS路由问题与Apache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的约曼软件包管理器创建一个角的Web应用程序。这是我的主控制器:

 使用严格的;

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


  ])
  的.config(函数($ routeProvider,$ locationProvider){
    $ routeProvider
      。什么时候('/', {
        templateUrl:意见/ main.html中,
        控制器:MainCtrl
      })

      。当('/约',{
        templateUrl:意见/ about.html',
        控制器:AboutCtrl
      })

      。当('/'接触,{
        templateUrl:意见/ contact.html',
        控制器:ContactCtrl
      })
      不然的话({redirectTo:'/'});

      //使用HTML5历史API
  //使用HTML5历史API
    $ locationProvider.html5Mode(真正的);
  });
 

如果我从基本目录访问应用程序,但如果我从URL访问它,去www.base.com/route~~V我得到一个404错误,工作正常。我的理解是,因为角是一个客户端应用程序,因此没有任何服务器来处理这些请求的。我已经看了很多网上关于必须重新路由与服务器或mod-rewrite与Apache,但我仍然没有得到它的工作。

的资料我都试过

<一个href="http://stackoverflow.com/questions/15385659/apache-rewrite-rules-not-being-applied-for-angularjs">Apache重写规则不被应用于angularjs

https://groups.google.com/forum/#​​!味精/角/ GmNiNVyvonk / mmffPbIcnRoJ

我的.htaccess的Apache Web服务器:

 #BEGIN angularJS
&LT; IfModule mod_rewrite.c&GT;
    RewriteEngine叙述上
    的RewriteBase /
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    重写规则^(。*)/index.html/#/$1
&LT; / IfModule&GT;
#END angularJS
 

解决方案

我试着重写了一些东西,不能让它的工作,但发现使用重定向和的 NE标志

  RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。+)#$ / $ 1 [NC,R,NE]
 

似乎有不少其他有趣的链接从这个SO回答有关散列/ URL片段: http://stackoverflow.com/ A / 15135130

I am using Yeoman package manager to create an angular web application. this is my main controller:

    '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);
  });

which works fine if I access the app from the base directory but if I access it from the url and go to www.base.com/route I get a 404 error. Which I understand is because Angular is a client side app so there is nothing on the server to handle these requests. I have read a lot online about having to reroute with server or mod rewrites with apache, but I still have not gotten it working.

Things I have tried

Apache rewrite rules not being applied for angularjs

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

my .htaccess for apache web server:

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

解决方案

I've tried a few things with rewriting and couldn't get it to work but found a workable solution using redirection and the NE flag

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

There seems to be quite a few other interesting links from this SO answer regarding the hash/ URL fragment: http://stackoverflow.com/a/15135130

这篇关于AngularJS路由问题与Apache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆