【问题标题】:How to get redirect from non-www to www on a Meteor app如何在 Meteor 应用程序上从非 www 重定向到 www
【发布时间】:2015-11-03 00:59:15
【问题描述】:

我正在尝试将所有非 www 流量重定向到我的 www 域。如果我输入example.com,它会重定向到正确的https://www.example.com。但是,如果我输入http://example.comhttps://example.com,我会得到一个无限重定向循环。

我已经使用 Namecheap 设置 URL 转发,并在 Heroku 上托管。

目前,我的服务器代码中有这个,它正在创建重定向循环(没有它我只会得到一个错误):

WebApp.connectHandlers
    .use(function(req, res, next) {
      var uri = new URI(req.originalUrl);
      if (uri.subdomain() != 'www') {
        uri.normalizeProtocol();
        res.writeHead(301, {
          'Location': 'https://www.example.com' + uri.resource()
        });
        res.end();
      } else {
        next();
      }
    });

我正在使用 URI.js 来解析 url。关于如何改变这一点的任何想法?

【问题讨论】:

    标签: node.js redirect heroku meteor subdomain


    【解决方案1】:

    我建议在 Namecheap 中禁用 URL 转发并直接在您的应用堆栈中处理。

    之后有几个选择:

    1. 如果您使用像 nginx 这样的代理来提供服务,那么这可能会有所帮助:
      How To Redirect www to Non-www with Nginx on CentOS 7

    2. 如果您直接提供服务,那么可能在路由器级别 - 在Iron Router

       Router.route("addWWW", {
            where: "server",
            path: "*",
            action: function() {
              var fullUrl, host;        
              host = this.request.headers.host;
      
              if (host.indexOf("www") !== 0) {
                fullUrl = "http://wwww." + host + this.request.url;
                this.response.writeHead(HTTP_REDIRECT_PERMANENT, {
                  Location: fullUrl
                });
                return this.response.end();
              }
            }
          });
      

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2010-11-09
      相关资源
      最近更新 更多