【发布时间】:2014-09-24 19:42:46
【问题描述】:
目前我正在使用 $routeProvider 来动态加载页面的各个部分,如下所示:
$routeProvider
.when('/', {
templateUrl: '/pages/home.html',
controller: 'mainController'
})
.when('/our-business', {
templateUrl: '/pages/our-business.html',
controller: 'businessController',
css: 'css/_business.css'
})
.when('/solutions', {
templateUrl: '/pages/solutions.html',
controller: 'solutionsController'
});
目前,如果我直接进入索引 (localhost),然后从导航菜单中选择“我们的业务”,那么 Angular 会处理位置请求并且页面加载正常,URL 更改为 localhost/our-业务。如果我然后重新加载或直接打开此 URL,我会收到 404 错误 - 可能是因为 Apache 试图打开不存在的 our-business.html。如果我打开 localhost/#/our-business 然后加载索引,然后 Angular 处理请求。我遇到的问题是,这是一个面向公众的网站,所以如果用户要复制和粘贴 URL 或通过电子邮件共享它,他们会收到 404 错误。
有没有办法让 Apache 重写 URL 以通过索引和 AngularJS 解析它们,这样我们就可以保持非散列样式但仍然有功能性 URL?
【问题讨论】:
-
你可以尝试使用 HTML5 模式,如 $locationProvider.html5Mode(true);。更多详情请参考this。
-
我遇到了同样的问题。这就是我修复它的方法:stackoverflow.com/a/21484874
标签: javascript angularjs apache angularjs-routing