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

标签 meteor

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

相关文章:

javascript - Meteor Resource 解释为样式表但使用 MIME 类型 text/html 传输

meteor - 您如何保护客户端MongoDB API?

javascript - 定制 Handlebars 助手

javascript - 客户端上的 Meteor.js 集合为空

javascript - Meteor.call 在 stub 内失败?

javascript - 模拟调用 'deals.insert'错误效果时出现异常

ios - 望远镜应用程序无法通过自定义 URL 进行移动响应

javascript - Cordova BarcodeScanner 连续读取

meteor - 调试 Meteor Velocity Mocha 测试

css - Bootstrap 无法与 meteor 一起正常工作