Next.js Discord

Discord Forum

Only get redirect on localhost but when building->starting app the redirection stops working.

Unanswered
Kuchi posted this in #help-forum
Open in Discord
Avatar
KuchiOP
  if (provinceCookie) {
    const originalPath = req.url; // Use req.url to get the full URL, including the hostname and protocol

    if (provinceCookie === "AB" && !originalPath.includes("/AB")) {
      const url = new URL(originalPath);
      if (!url.pathname.includes("/AB")) {
        url.pathname = `/AB${url.pathname}`;
        return NextResponse.redirect(url.toString());
      }
    }

    if (provinceCookie === "BC" && originalPath.includes("/AB")) {
      // Updated regular expression to match "/AB" or "/AB/" at the start of the pathname
      const regex = /\/AB\/?/;

      if (regex.test(originalPath) && originalPath !== "/") {
        const redirectedPath = originalPath.replace(/\/AB\/?/, "/"); // Replace "/AB/" or "/AB" with "/"
        return NextResponse.redirect(redirectedPath);
      }
    }

    // If the user has a value for the provinceCookie, simply return the requested path
    return NextResponse.next();
  }

2 Replies

Avatar
KuchiOP
seems like NextResponse.redirect(redirectedPath) just doesnt redirect
Image
even when the url is correct