platforms
platforms copied to clipboard
Middleware rewriting for /home/ on Vercel returning Not Found
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.
First time deployment gives me the same error too, what's the proposed fix here?
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 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?
@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?
Merged #323 which should fix this! Sorry for the delay!