我想知道是否有人可以告诉我在 Angular js 中将模式更改为 html5 时需要进行哪些服务器端更改。因为当我尝试将其更改为 html 5 模式时,我无法进入我的内部 html 页面。在 Angular API 中,它说用户也需要进行服务器端更改。
- 服务器端有哪些变化
- 我们还需要做任何其他更改吗?
您应该区分两种类型的调用:
如何区分这两种类型的调用,以及如何重新映射前者,很大程度上取决于您的设置。
例如,如果您使用的是 nginx,则检查 $http_accept 的组合是application/json
(请参阅http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests和http://wiki.nginx.org/HttpCoreModule#.24http_HEADER)并重写(http://wiki.nginx.org/HttpRewriteModule#rewrite)你可以实现你想要的。
您需要设置服务器以将所有内容重写为 index.html:https ://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to -work-with-html5mode ...
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
我的应用程序已将参数传递给控制器(通过 ui-router),因此在 html5mode 之前我会转到 www.blah.com/angapp/#/myUIrouterController?param_x=1¶m_y=2 猜测浏览器知道 /#/ 文件夹路径部分应该提供索引.html。
现在 # 将与 html5mode 一起消失,默认情况下服务器不知道为该文件夹提供 index.html,因为 url 将只是: www.blah.com/angapp/myUIrouterController?param_x=1¶m_y=2 myUIRouterController 是'不是一个真正的文件,所以服务器只会提供 404,因此为什么我认为需要重写,所以它知道将所有内容发送到 index.html(所以上面与 <base> 标记的组合应该可以工作......注意: requireBase 是可选的,但听说它可能有助于像 IE9 这样的旧浏览器)。