【发布时间】:2014-07-02 00:25:27
【问题描述】:
我想将进入我网站的裸域的用户自动重定向到 www 子域。 Pre-Blaze,这是我使用 Iron Router 执行此操作的代码:
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 和 Iron Router 的 Blaze 兼容版本时,这停止了工作。 (我用 pause() 替换了 this.stop(),但这没有帮助。)现在,当您导航到裸域时,页面只是挂起,没有控制台错误。
是否有可靠且与 Blaze 兼容的方法将用户从裸域重定向到 www? (或者也许这根本不应该在应用程序级别?)
这是我的 Blaze 后代码。 (这是当前不工作的代码。)
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();
}
});
只用一点额外信息进行编辑:我刚刚发现,对于裸域,传递给 Router.onBeforeAction() 的函数根本无法运行。
【问题讨论】:
-
如果你在 NGINX 反向代理后面运行 Meteor,在 Nginx 中进行域重定向非常容易。
标签: meteor iron-router