gpt4 book ai didi

meteor - 如何在 meteor.js 应用程序中从非 www 重定向到 www

转载 作者:行者123 更新时间:2023-12-01 09:59:10 24 4
gpt4 key购买 nike

我是 meteor 的新手。从

自动重定向会很酷

example.com

www.example.com

.谁能帮忙?

最佳答案

我知道这是 2 年前的事了,但没有公认的答案,所以我提供了一个完整的答案:

WebApp.connectHandlers.use(function(req, res, next) {

// Check if request is for non-www address
if (req.headers && req.headers.host.slice(0, 4) !== 'www.') {

// Add www. from host
var newHost = 'www.' + req.headers.host

// Redirect to www. version URL
res.writeHead(301, {
// Hard-coded protocol because req.protocol not available
Location: 'http://' + newHost + req.originalUrl
});
res.end();

} else {
// Continue with the application stack
next();
}
});

您可以使用以下代码转到相反的方向(www 到非 www):

WebApp.connectHandlers.use(function(req, res, next) {

// Check if request is for non-www address
if (req.headers && req.headers.host.slice(0, 4) === 'www.') {

// Remove www. from host
var newHost = req.headers.host.slice(4);

// Redirect to non-www URL
res.writeHead(301, {
// Hard-coded protocol because req.protocol not available
Location: 'http://' + newHost + req.originalUrl
});
res.end();

} else {
// Continue with the application stack
next();
}
});

关于meteor - 如何在 meteor.js 应用程序中从非 www 重定向到 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com