Javascript 主干路由器&x2B;重写不触发路由的规则/&引用;
我将所有链接从Javascript 主干路由器&x2B;重写不触发路由的规则/&引用;,javascript,.htaccess,backbone.js,router,Javascript,.htaccess,Backbone.js,Router,我将所有链接从example.com/action转换为example.com/index.html#action,然后由骨干路由器解析 但是,我的新页面,signup/:inviteAuthHash(例如signup/9d519a2005b3dac8802d8e9e416239a1)不起作用;唯一呈现的是我的index.html,并且我的路由器中没有断点 .htaccess: # not a file or directory RewriteCond %{REQUEST_FILENAME} !
example.com/action
转换为example.com/index.html#action
,然后由骨干路由器解析
但是,我的新页面,signup/:inviteAuthHash
(例如signup/9d519a2005b3dac8802d8e9e416239a1
)不起作用;唯一呈现的是我的index.html
,并且我的路由器中没有断点
.htaccess:
# 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
};
你能试试这个.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]
在我看来,还有一个更好的办法可以解决你的问题:使用pushState 所以你要做的是
Backbone.history.start({pushstate:true,root://“})
,请参阅在支持pushstate的浏览器上,url将按其应有的方式显示,一切正常,在较旧的浏览器上,主干会自动将其转换为带有散列的url。我的mod_rewrite有点生疏,但
RewriteRule
确实会将301或303发送回浏览器,还是内部重定向?@Garrett我想注册/9D519A2005B3DA8802D8E416239A1
会转换为urlindex.html/#signupindex.html/#9d519a2005b3dac8802d8e9e416239a1
,因为有两个/
,//code>。我可能也错了。另一个疑问是,您的邀请哈希是否包含字符:
?那么这就不会了work@muistooshort-这取决于您是否使用[R]标志。如果不是,则它将透明地通过隧道(客户端收到200条)。#表示.htaccess中的注释。这是造成问题的原因吗?@AdityaManohar:我没有看到[R]
,这意味着它是一个内部重定向,客户端将永远看不到index.html#……
,没有哈希意味着不会发生客户端路由。假设#
没有像@Scott建议的那样在该上下文中被视为注释。这会将我重定向到C://
目录下的本地文件。(?!^index\.html)
有什么作用?(?!^index\.html)
是一个负先行正则表达式,它确保只有在URI开始时没有index.html
时才转发到index\.html
。你能试试看:RewriteRule(?^index\.html)^(+$/index.html#$1[L,NC,R]
啊,我明白了。对于像“我的仪表板”这样的页面,/dashboard
,它会重定向到/index.html%23dashboard
(无效url)。我试图逃离#
,但没有成功。当我删除R
时,它又开始为普通URL工作,但没有修复原始问题(对于超过1个/
的URL)。嗯,我认为您需要一个额外的NE
标志。现在请检查我编辑的答案。此规则现在将/foo/bar/baz
重定向到/index.html#foo/bar/baz
。另外,我相信/dashboard
不是DOCUMENT\u ROOT下的物理目录(因为我们使用RewriteCond%{REQUEST\u FILENAME}!-d
条件来避免它们),谢谢NE
是丢失的确切标志!虽然index.html
部分给主干网带来了问题,但将其更改为RewriteRule(?!index\.html)^(+)$/#$1[L,NC,R,NE]
可以工作。不幸的是,这确实会为后面的每个链接重新加载页面(它没有使用pushState
,但我现在可以不使用它。重要的是它可以工作。我已经在做(1)和(2)。我没有根:“/”
所以我添加了它,但是/signup/6efa34df09a031a018dc7627dc8509f7
仍然不起作用-我必须将其更改为#signup/6efa34df09a031a018dc7627dc8509f7
。