angularjs html5mode 刷新页面获取 404 [英] angularjs html5mode refresh page get 404
问题描述
我使用 AngularJS 和 GAE(Google App Engine – Java)创建了一个应用.
我想将其转换为与 SEO 兼容.
index.html
并且body通过
动态加载.
app.js
$locationProvider.html5Mode(true);
网址工作正常,但当我刷新页面时出现 404 错误.
你知道这是什么原因吗?
我相信你已经解决了这个问题,但对于遇到这个问题的其他人:
基本上 AngularJS 的 $locationProvider.html5mode(true)
使用了 HTML5 的 history.pushState()
,它在不重新加载页面的情况下人为地改变了用户的历史记录和地址栏 URL.如果您为 /about
创建了一个路由(在 Angular 中),但在服务器上没有任何匹配的路由,您将遇到以下问题:重新加载页面会显示它不存在的事实服务器.最简单的解决方案是为应用可访问的所有路由镜像应用入口点 (/index.html
?).
参见:https://stackoverflow.com/a/16570533/1500501
I've created an app using AngularJS with GAE (Google App Engine – Java).
I want to convert it compatible with SEO.
index.html
<meta name="fragment" content="!" />
<base href="/">
and the body is loaded dynamically through <div ui-view></div>
.
app.js
$locationProvider.html5Mode(true);
The url works fine but when I refresh page I get 404 error.
Do you have any idea, what this causes?
I'm sure you've already solved this, but for anybody else running into this issue:
Basically AngularJS's $locationProvider.html5mode(true)
makes use of HTML5's history.pushState()
, which artificially changes the user's history and address bar URL without reloading the page. If you create a route (in Angular) for /about
, but don't have any matching route on the server, you will run into the issue where reloading the page reveals the fact that it's not there on the server. The simplest solution is to mirror your entry point for your app (/index.html
?) for all routes accessible by your app.
See: https://stackoverflow.com/a/16570533/1500501
这篇关于angularjs html5mode 刷新页面获取 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!