3

我转换所有链接,example.com/action然后example.com/index.html#action由我的主干路由器解析。

但是,我的新页面signup/:inviteAuthHash(例如signup/9d519a2005b3dac8802d8e9e416239a1)无法正常工作;唯一呈现的是我index.html的,我的路由器中没有一个断点被满足。

.ht 访问:

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# example.com/home => example.com/index.html#home
RewriteRule ^(.+)$ index.html#$1 [L,QSA]

路由器.js

var AppRouter = Backbone.Router.extend( {
    routes : {
        /* PAGES */
        // beta
        'request_invite' : 'request_invite',
        'signup/:inviteAuthHash' : 'signup',

        // Default
        '*actions' : 'defaultAction'
    },

    /* PAGES */
    // eta
    request_invite : function() {
        new BetaLayout;
        new RequestInviteView;
    },
    signup : function(inviteAuthHash) {
        // validate auth hash
        if(!InviteRequest.isValidInviteAuthHash(inviteAuthHash))
            return this.defaultAction();
        else {
            new BetaLayout;
            new SignupView(SignupView);
        }
    },

    // Default
    defaultAction : function(actions) {
        app_router.navigate('request_invite');
        this.request_invite();
    }
});

var initialize = function() {
    app_router = new AppRouter;

    $('a').live('click', function() {
        var href = $(this).attr('href');

        // only navigate to real links
        if(href == undefined)
            return;

        // open in new window?
        if($(this).prop('target') == '_blank')
            return true;

        app_router.navigate(href, {trigger: true});

        return false;
    });

    Backbone.history.start({pushState: true});
};
return {
    initialize : initialize
};
4

2 回答 2

2

你可以试试这个 .htaccess 吗:

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# example.com/home => example.com/index.html#home
RewriteRule (?!^index\.html)^(.+)$ /index.html#$1 [L,NC,R,NE]
于 2012-06-12T14:01:11.217 回答
0

我认为还有另一种,在我看来更好的解决您的问题的方法:使用 pushState。

所以你要做的是

  1. 更改您的服务器配置,以便除您的资产之外的 example.com 下的所有 url 实际上呈现 index.html(我的 apache 知识生疏,但这应该不难做到)。
  2. 更改对 Backbone.history.start 的调用以启用 pushstate,并指定根Backbone.history.start({pushState: true, root: "/"}),请参阅Backbone 文档

在支持 pushstate 的浏览器上,url 将按原样显示并且一切正常,在较旧的浏览器上,主干将自动将其转换为带有哈希的 url。

于 2012-06-14T08:20:15.267 回答