0

我是 angularJS 的新手,我喜欢用它编码。我目前正在使用 angularJS 构建一个 Web 门户,并且我的每个页面都使用ngRoute. 它对我来说很好,但是当我尝试刷新页面时,它返回给我一个404 error / page not found. 在这个问题上我应该怎么做?这是我的代码:

var app = angular.module("CVSRRC", ['ngMaterial','ngRoute']);

app.controller('CVSRRC-CTRL', function($scope, $http, ...){
    // some codes here...
});

app.config(function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(true).hashPrefix('!');

    $routeProvider
    .when('/', {
        templateUrl: '_pages/home',
        controller: 'homeCTRL',
        resolve: {
           delay: function($q, $timeout){
                var delay = $q.defer();
                $timeout(delay.resolve, 1000);
                return delay.promise;
           }
        }  
    })
    ...etc...
    .otherwise({
         redirectTo: '/'
    });
});

在我的HTML中

<html>
   <head>
       <base href="/cvsrrc/" />
   </head>
   <body ng-app="CVSRRC" ng-controller="CVSRRC-CTRL">

      <div class="row" id="main">
        <div ng-view ng-show="statechange"></div>
        <div ng-show="!statechange" cvsrrc-resolve>
            <div id="_loader_"></div>
        </div>
    </div>

   <a href="about-us">About</a>
   <a href="login">login</a>
   //etc
   </body>
 </html>

这是我的 .htaccess 看起来像

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 http://localhost/cvsrrc/page-not-found
4

3 回答 3

1

由于您已使用<base href="/cvsrrc/" />并且$locationProvider.html5Mode(true).hashPrefix('!');您的网址已删除 #! 从 url 并添加 cvsrrc/. 因此,如果您从应用程序重定向应用程序,它将起作用。

但是当您重新加载页面时,它会尝试使用带有 cvsrrc/ 的 url 来查找实际不存在的路径。

所以你必须在你的服务器上使用 rewrite 模块来告诉重定向 cvsrrc/ 到 #!/

于 2017-01-30T07:12:39.107 回答
0

尝试:

  1. 从您的路线中删除解决方案。
  2. 在配置后添加以下代码:

    $locationProvider.hashPrefix("");

  3. 分享您用于访问该页面的 url。

  4. 删除基地中的结尾“/”。

于 2017-01-30T09:27:12.547 回答
0

最后我找到了一个解决方案,我之前已经遇到过这个解决方案,但是它对我不起作用,因为我忘记/localhost/.htaccess! 这里是:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^ - [L]

    RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>
于 2017-01-31T03:17:24.673 回答