使用Iron路由器将Meteor中的裸域重定向到www
我想自动重定向用户谁进入我的网站裸体域到www子域。Pre Blaze,以下是我使用Iron Router执行此操作的代码:使用Iron路由器将Meteor中的裸域重定向到www,meteor,iron-router,Meteor,Iron Router,我想自动重定向用户谁进入我的网站裸体域到www子域。Pre Blaze,以下是我使用Iron Router执行此操作的代码: Router.onBeforeAction(function() { var rootDomainRegex = new RegExp( '^https:\\/\\/mysite.com', 'ig' ); if ( window && rootDomainRegex.test( window.loc
Router.onBeforeAction(function() {
var rootDomainRegex = new RegExp(
'^https:\\/\\/mysite.com',
'ig'
);
if ( window && rootDomainRegex.test( window.location.href ) ) {
this.stop();
window.location = window.location.href.replace(
rootDomainRegex,
'https://www.mysite.com'
);
}
};
当我升级到Meteor 0.8.0和与Blaze兼容的Iron Router版本时,它就停止工作了。(我用pause()替换了这个.stop(),但这并没有帮助。)现在,当您导航到裸域时,页面只是挂起,没有控制台错误
是否有人有一种可靠且兼容Blaze的方法将用户从裸域重定向到www?(或者这根本不应该是应用程序级别的?)
这是我的发帖代码。(这是当前不起作用的代码。)
用一些额外的信息进行编辑:我刚刚发现,对于裸域,传递给
路由器.onBeforeAction()
的函数根本不运行。您应该粘贴更新的代码,并将其替换。请暂停
我怀疑有任何blaze与您的代码有关,它应该可以正常工作,即使您不调用pause,也应该重定向您。所以这可能是因为您的正则表达式不匹配
我猜您试图在if
语句之前调用console.log(rootDomainRegex.test(window.location.href))
,它返回了true
?!如果您这样做了,这就是原因,第一次调用regex.test时,它匹配返回的true,但也会增加regex上的lastIndex-因为g
标志,第二次调用test
时,它尝试从该lastIndex进行匹配,但失败了,从未执行window.location
部分
HTH,如果没有,请尝试粘贴您现在无法使用的确切代码。如果您在NGINX反向代理后运行Meteor,在NGINX中执行域重定向非常容易。谢谢,我刚刚编辑添加了当前的实时代码。正则表达式没有在其他任何地方被
test()
ed,但为了安全起见,我添加了一个lastIndex
reset。但这没用。
Router.onBeforeAction(function(pause) {
var rootDomainRegex = new RegExp(
'^https:\\/\\/mysite.com',
'ig'
);
rootDomainRegex.lastIndex = 0;
if ( window && rootDomainRegex.test( window.location.href ) ) {
window.location = window.location.href.replace(
rootDomainRegex,
'https://www.mysite.com'
);
pause();
}
});