骨干.js和模组重写

Backbone.js and mod rewrite

本文关键字:重写 js 骨干      更新时间:2023-09-26

所以我正在玩骨干,并且已经到了加载使用 pushState 的直接页面无法正常工作的地方。 如果我尝试转到 my.url.com/login 它会给我一个未找到的页面,因为它直接不存在。 我有以下重写规则:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [L,QSA]

这似乎不起作用(认为我得到了一个错误的请求而不是没有找到)。 如何让 pushState url 通过 mod 重写正确加载?

这实际上是我第一次阅读新的历史 API 以及 Backbone 如何使用它 - 非常酷的东西。

要解决错误请求问题,您需要做的就是在index.html之前添加/。就我而言,它会在内部正确重定向并按您的预期传递查询字符串。

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.html [L,QSA]