我是 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/