Simple 301 redirect in Meteor?


#1

I’m looking for a simple example of a 301 redirect in Meteor.

For example: I want https://domain.com/foo to 301 redirect to https://domain.com/bar

I’m using FlowRouter if that’s at all relevant.


#2

Here is a piece of server-side code from our app for you to customize

    WebApp.connectHandlers.use(function(req, res, next) {
      var path = req._parsedUrl.path;
      // console.log(req);
      if (path == "/") {
        // temporary redirect - https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
        res.writeHead(307, {Location: '/landing'});
        res.end();
      }
      else next.apply();
    });

#3

@ramez is right, as that’s the only way to get a response code, and also saves you from flicker when you refresh the page or arrive to the route from an external site. FlowRouter only works in the frontend.

However, for a smooth user experience, you should also ensure frontend redirection from ‘/foo’ to ‘/bar’. More info here: https://github.com/kadirahq/flow-router#redirecting-with-triggers

Here is an excellent resource, highlighting how server-side routing can work hand in hand with client-side routing: https://guide.meteor.com/routing.html