【问题标题】:Meteor Iron Router Catch-All 301 RedirectMeteor Iron 路由器 Catch-All 301 重定向
【发布时间】:2015-04-06 07:03:19
【问题描述】:

我正在将应用程序从网站的根域迁移到子域。根域上的新应用程序是用 Meteor 编写的,并使用 Iron-router。为了避免破坏外部链接,我想将所有未被流星应用处理的 URL 301 重定向到子域。

我似乎无法弄清楚如何使用 Iron-router 来做到这一点。由于我正在执行 301 重定向,因此它是一个服务器路由(通过 {where: 'server'}),但是当我这样做时,通用路由优先于所有其他现有路由,即使它在我的路由文件。

版本:

流星 1.0.3.1

铁:路由器 1.0.7

Router.route('/', function () {
  this.render('home');
});
Router.route('/about', function () {
  this.render('about');
});

// Takes precedence over all above routes (due to server?)
Router.route('/(.*)', function () {
  this.response.writeHead(301, {'Location': 'https://subdomain.thedomain.com/' + this.params[0]});
  this.response.end();    
}, {where: 'server'});

【问题讨论】:

    标签: redirect meteor iron-router


    【解决方案1】:

    我认为您的路线模式是错误的。请尝试以下模式:

    Router.route('/:path?', function () {
      this.response.writeHead(301, {'Location': 'https://subdomain.thedomain.com/' + this.params.path});
      this.response.end();    
    }, {where: 'server'});
    

    ?表示该参数是可选的。

    【讨论】:

      【解决方案2】:

      看起来这是实现目标的更好方法:

      WebApp.connectHandlers
        .use('/url_to_redirect', function(req, res, next) {
          // 301 Moved Permanently
          res.writeHead(301, {
            'Location': '/url_where_we_want_to_go'
         });
          res.end();
        });
      

      有关此的更多信息:http://meteorpedia.com/read/HTTP_Redirects

      【讨论】:

        猜你喜欢
        • 2015-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-03
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多