2016-05-28 50 views
1

我使用ui.router與angularJs,我遇到了奇怪的行爲。 如果我點擊槽頁面一切正常工作,鏈接正在改變,模板正在加載,但如果我直接訪問某些特定頁面(其他然後www.myapp.com)我總是得到404頁面找不到

這是我的app.js

var app = angular.module('app', ['ui.router', 'ui.bootstrap']); 

app.config(function($stateProvider, $urlRouterProvider,$locationProvider){ 
    $urlRouterProvider.otherwise("/dashboard"); 
    $stateProvider 
     .state('dashboard', { 
      url: "/dashboard", 
      templateUrl: "app/templates/dashboard/dashboard.html", 
      controller: 'dashboardCtrl' 
     }) 
     .state('locations', { 
      url: "/locations", 
      templateUrl: "app/templates/locations/locations.html", 
      controller: 'locationsCtrl' 
     }) 
     .state('locations.add_location', { 
      url: "/locations/add_location", 
      templateUrl: "app/templates/locations/add_location.html", 
      controller: 'addLocationCtrl' 
     }); 
    $locationProvider.html5Mode(true); 
}); 

解釋圖片

我訪問www.myapp.com,頁面呈現 enter image description here
我點擊的位置,一切工作正常 enter image description here
我也可以點擊儀表盤鏈接再次,它會工作

但是,如果我按F5或複製在瀏覽器的地址欄中粘貼鏈接,我會得到404 enter image description here

這是爲什麼發生。我即將失蹤。如果您需要任何其他信息,請讓我知道,我會提供。

回答

2

這是因爲Apache試圖爲您的www根目錄中的文件location提供服務。這是不可能的,因爲你有沒有子頁面的SPA應用程序。您必須強制Apache Web服務器重寫所有位置並提供index.html頁面(主頁)。我發現有用的要點: https://gist.github.com/santthosh/7dabf08fa3859361ef1e

+0

該文件應放在我的應用程序的根目錄嗎? –

+0

最簡單的將是htaccess,如果沒有完全訪問php – charlietfl

+0

我可以做到這一點,改變php或寫入htaccess文件。你有什麼建議? –