Backbone.js PushState routes .htaccess only working as hash but nowhere else

好久不见. 提交于 2019-12-23 16:49:57

问题


I have a website domain.com for example. I have backbone.js with pushstate and fallback and when I goto domain.com/about it loads up the index.html page and pushstates to about. everything is working. but if i want to goto a directory with a page inside like: www.domain.com/bio/moreinfo for example, it does not work and throws a invalid page. if i do it in IE it works fine. my htaccess file has the following:

RewriteEngine on
# html5 pushstate (history) support: 
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>

if i navigate directly to the page domain.com/bio/moreinfo it craps out (i think because my server wants to goto the bio directory? or maybe I need to actually control the routes in backbone differently? its just that it works on bangs so it has to be some weird push state directory thing where #bio/info is not the same to apache as /bio/info . any help is appreciated.


回答1:


well using the answer in another suggested post, which was to do <base href="/" /> in the index.html file. This actually made the sub directories in my pushState work! just it was suggested.. but then in return it broke my IE, but i fixed it by putting extra code in my INIT of backbone

 Backbone.history.start({ pushState: Modernizr.history, silent: true });
if(!Modernizr.history) {
    var rootLength = Backbone.history.options.root.length;
    var fragment = window.location.pathname.substr(rootLength);
    var search = window.location.search;
    Backbone.history.navigate('/#' + fragment + search, { trigger: true });
} else {
    Backbone.history.loadUrl(Backbone.history.getFragment())
}


来源:https://stackoverflow.com/questions/14483076/backbone-js-pushstate-routes-htaccess-only-working-as-hash-but-nowhere-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!