Fix WhatsApp infinite redirection starting with double slashes

I have a web server with routes such as:

router.get("/inicio", async (req, res) => {
  // logic ...
  return res.render("template");
}

and a route for everything else:

app.get("*", async (req, res) => {
  await authentication.getLocalInfo(req);
  return res.redirect("/404");
});

When I share my website on WhatsApp with `https://emocoes.org/inicio”, the server logs the following requests in quick succession, with about 70 MB of text in the server logs for this near-infinite loop:

2022-03-06T10:26:16.176Z - //inicio 
2022-03-06T10:26:16.334Z - //inicio/404 
2022-03-06T10:26:16.492Z - //inicio/404/404 
2022-03-06T10:26:16.652Z - //inicio/404/404/404 
2022-03-06T10:26:16.801Z - //inicio/404/404/404/404 
2022-03-06T10:26:17.027Z - //inicio/404/404/404/404/404 
2022-03-06T10:26:17.191Z - //inicio/404/404/404/404/404/404 
2022-03-06T10:26:17.329Z - //inicio/404/404/404/404/404/404/404 
2022-03-06T10:26:17.520Z - //inicio/404/404/404/404/404/404/404/404 
2022-03-06T10:26:17.789Z - //inicio/404/404/404/404/404/404/404/404/404 
2022-03-06T10:26:17.955Z - //inicio/404/404/404/404/404/404/404/404/404/404 
2022-03-06T10:26:18.093Z - //inicio/404/404/404/404/404/404/404/404/404/404/404 
...

Another example: /diadopai should redirect to /registo but instead redirects to /diadopai/registo, which does not exist, and so on.

How can I make WhatsApp request the right route, or avoid this near-infinite loop?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your redirects are not absolute, and are using the current path as the base. Try this instead:

return res.redirect(req.protocol + "://" + req.headers.host + "/404");

Also, you should create a route to handle /404 otherwise it will infinetly loop back on itself. This should display an error page etc and should not redirect anywhere new.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x