Search code examples
javascriptnode.jsexpresshttp-redirecthttp-status-code-301

How show I handle a 301 redirect from an existing page to another page in my index.js file


Hi everyone and happy friday! Im just trying to find out what is the best way to handle a 301 redirect in node from an existing file to another file. Lets say I have a file that exists called /contact

router.get('/contact', function (req, res) {
  res.render('contact');
});

If I want to redirect users from that file onto a new route on wordpress, which is /to/contact should I do this?

router.get('/to/contact', function (req, res) {
  res.redirect(301).('contact');
});

I tried doing research on this but not sure what to do for a explicit redirect like here. Its a bit confusing. Thanks!


Solution

  • You've got the two routes confused:

    router.get('/contact', function (req, res) {
      res.redirect(301, '/to/contact');
    });