我正在编写一个使用HTML5历史API的Backbone.js应用程序。 我希望用户能够创build表单的URL:
domain.com/any domain.com/random domain.com/paththattheuserlikes
并将所有这些URL路由到我的index.html页面,其中骨干路由器将采取适当的path和处理请求。
我的问题是:我如何设置Apache将所有请求路由到index.html ,同时保持path,以便骨干路由器将正确处理请求?
我知道如何做一个简单的Apacheredirect,但我担心会删除path。
在你的Apacheconfiguration文件中,放入以下几行:
RewriteEngine On RewriteRule ^/[a-zA-Z0-9]+[/]?$ /index.html [QSA,L]
这会将由字母数字字符组成的所有请求重写为index.html,同时仍保留查询string, 并仍然显示为与键入的path相同。 因此,如果用户转到了yourdoma.in/someoldpath,将显示index.html,但地址栏仍然会显示yourdoma.in/someoldpath。
正如第一张海报所提到的,如果您想知道input的path,可以将上面的第二行更改为:
RewriteRule ^/([a-zA-Z0-9]+)[/]?$ /index.html?pathtyped=$1 [QSA,L]
哪个会传递原始path在“pathtyped”请求variables中键入index.html。
如果您不想丢失原始path,则需要在目标页面的查询string中包含它们(或replace),以便能够获取和处理它们。