问题描述
而我注意到一个问题,我设置了一个简单的路由器在Backbone.js的,我用Google搜索了半天,发现了一些解决方案。
I was setting up a simple router in Backbone.js while I noted an issue, I googled for a long time and found some solutions.
问题是,每当我浏览到一个URL,这个网址将工作,而我在应用程序本身内部,但一旦你加入收藏夹,并返回它显然会返回404,因为该页面不存在。
The problem is that whenever I navigate to a url, this url will work while I'm inside the app itself, but once you bookmark it and return it will obviously return a 404 since that page doesn't exist.
Backbone.history.navigate('test', true);
这将导航到 http://www.domain.com/test
,这是伟大的,但一旦你刷新相同的URL它返回一个404。
This will navigate to http://www.domain.com/test
, which is great, but once you refresh the same URL it returns a 404.
有办法得到这个使用特定的的.htaccess
文件的工作,但我建立这个现有的网站里面,所以我不能用这个
There are ways to get this to work with using a specific .htaccess
file, but I'm building this inside an existing website, so I can't use this.
有没有Backbone.js的方式回复到只是用hashbangs?那么我的网址看起来像这样` http://www.domain.com/#!/test一>
这可能不是最干净的URL,但它至少会工作。
Is there a way in Backbone.js to revert to just using hashbangs? My url would then look like this `http://www.domain.com/#!/testThis might not be the cleanest URL, but at least it will work.
还是我缺少明显的东西吗?它不能是我的唯一的人这个问题。
Or am I just missing something obvious here? It can't be that I'm the only person with this problem.
推荐答案
您正在使用 pushState的
根据您的网址,所以你有两个主要选择:
You're using pushState
according to your url, so you've got 2 main options:
- 停止使用
pushState的
起骨干历史(这将是使用hashbang相当于,除了SEO)时 - 您的服务器为每个您的应用程序生成的URL返回一个页面(也可以是完全相同的网页)
- stop using
pushState
when starting Backbone history (which would be the equivalent of using a hashbang, except for SEO) - have your server return a page for every URL your app generates (it can be the same exact page)
在第二种情况下,如果你不关心搜索引擎优化,你可以有一个包罗万象的规则总是返回(例如) index.html的
包含您的主干应用程序。当您启动主干的历史,它将lokk在URL并导航到正确的页面(虽然你可能有一个双重渲染效应)。
In the second case, if you don't care about SEO, you can have a catch all rule always returning (e.g.) index.html
containing your Backbone app. When you start Backbone's history, it will lokk at the URL and navigate to the proper page (although you might have a "double render" effect).
如果你担心SEO,你需要与你的主干应用程序一起返回的搜索引擎优化页面内容。
If you're worried about SEO, you need to return page content for SEO along with your Backbone app.
这篇关于骨干路由器:使用hashbangs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!