How to redirect abc.vercel.app/login to abc.vercel.app/ after refresh

Hello,

I am facing an issue with a 404: NOT_FOUND error in my app deployed to Vercel (everything works fine locally). My app is a React/Node.js application, and I use a frontend router to handle page navigation.

Here is a simplified version of my routing code:

<Router>
  <Routes>
    <Route element={<ProtectedRoute />}>
      <Route path="*" element={<AppPage />} />
    </Route>
    <Route path="/login" element={<LoginPage />} />
  </Routes>
</Router>

In the app, when the user doesn’t have valid credentials, they are redirected to the /login page where they can enter their username and password.

Everything works fine in the browser when navigating to abc.vercel.app. If the user has the correct credentials, they are directed to abc.vercel.app/, and they see the <AppPage /> component. If not, they are redirected to abc.vercel.app/login where the <LoginPage /> is displayed.

However, the issue arises when I manually type abc.vercel.app/login into the browser or refresh the page while already on the login page (abc.vercel.app/login). In this case, I receive a 404: NOT_FOUND error from Vercel.

What I have tried so far:

Backend Redirection:
I tried setting up redirects in my backend using Express, but it didn’t solve the issue.

app.get("/login", (req, res) => {
  res.redirect("/");  // Redirect GET requests from /login to the root ("/")
});

app.post("/login", (req, res) => {
  res.redirect("/");  // Redirect POST requests from /login to the root ("/")
});

Vercel Redirects:
I also added the following rewrite rule in my vercel.json, but this didn’t work either

"rewrites": [
  {
    "source": "/login",
    "destination": "/"    <- also with /index.html etc 
  }
]

I’m unsure where the problem lies. I would appreciate any suggestions or guidance on how to resolve this issue. Thank you!

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

Sorry for the confusion, the rewrites in vercel.json file works, but it needs to be added to the frontend, not the backend (where I mistakenly added it). You can close/delete the topic.

1 Like

Thanks for sharing the solution!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.