platforms icon indicating copy to clipboard operation
platforms copied to clipboard

Middleware rewriting for /home/ on Vercel returning Not Found

Open VictorCalderon opened this issue 1 year ago • 2 comments

Middleware redirection not working for ROOT_DOMAIN to /home

For reason I have yet to discover, every time I go to the NEXT_PUBLIC_ROOT_DOMAIN, I get a not found 404. The following snippet seemed to have fixed the issue. If @steven-tey could shed some light on this issue, I would be delighted.

if (
    hostname === "localhost:3000" ||
    hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
  ) {
    return NextResponse.rewrite(
      new URL(`/home${path === "/" ? "" : path}`, req.url), // <- based on previous rewrite rules.
    );
}

If this was not intended, I could open a PR right away.

VictorCalderon avatar Jul 05 '23 12:07 VictorCalderon

First time deployment gives me the same error too, what's the proposed fix here?

mattquestions avatar Jul 11 '23 10:07 mattquestions

Check that you've got the correct redirect rules configured for your domain.

You may have your apex domain (NEXT_PUBLIC_ROOT_DOMAIN) redirecting to www.NEXT_PUBLIC_ROOT_DOMAIN.

Either change the domain configuration to redirect www.NEXT_PUBLIC_ROOT_DOMAIN to NEXT_PUBLIC_ROOT_DOMAIN in your project Settings > Domains (vercel) or your DNS provider if you're doing it there. Or, modify the check to:

  // rewrite root application to `/home` folder
  if (
    hostname === "localhost:3000" ||
    hostname === `www.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`
  ) {
    return NextResponse.rewrite(new URL(`/home${path}`, req.url));
  }

Note that the installation guide suggests not adding the www.NEXT_PUBLIC_ROOT_DOMAIN domain, serving the website from the apex domain, and the wildcard domain picks up the rest.

davidmturner avatar Jul 14 '23 20:07 davidmturner

@davidmturner is exactly right!

You'll need to make sure that you are visiting the apex domain (not the www. version of it).

Does this problem occur locally too?

steven-tey avatar Jul 20 '23 20:07 steven-tey

@davidmturner thank you! I had everything set up properly as @steven-tey described in the installation guide.

@davidmturner is exactly right!

You'll need to make sure that you are visiting the apex domain (not the www. version of it).

Does this problem occur locally too?

Somehow it didn't! Which would suggest something about my APEX domain was not set properly. Is there any other consideration besides not redirecting to www?

VictorCalderon avatar Jul 25 '23 12:07 VictorCalderon

Merged #323 which should fix this! Sorry for the delay!

steven-tey avatar Sep 29 '23 21:09 steven-tey