How to redirect to home page instead of 404 page? #16749
Replies: 18 comments 27 replies
-
Hey @ahmedbeheiry, use nextjs redirects feature (https://nextjs.org/docs/api-reference/next.config.js/redirects), perfect for your case. |
Beta Was this translation helpful? Give feedback.
-
I've found this to be a good solution to this! In
|
Beta Was this translation helpful? Give feedback.
-
Seems odd there isn't a way to accomplish this using the |
Beta Was this translation helpful? Give feedback.
-
This can also work, I'm not sure if it's the optimal solution though. export default function Custom404() {
return null;
}
export const getStaticProps = () => {
return {
redirect: {
destination: '/',
},
};
}; |
Beta Was this translation helpful? Give feedback.
-
Typescript: import { GetStaticProps } from 'next';
export const getStaticProps: GetStaticProps = async () => {
return {
redirect: {
destination: '/',
permanent: true,
},
};
};
export default getStaticProps; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi, One way to do this is to combine /** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
async rewrites() {
return {
afterFiles: [{ source: "/:path*", destination: "/_404/:path*" }],
};
},
};
module.exports = nextConfig; With this file: // pages/_404/[...path].tsx
// renders nada
export const Custom404 = () => <div></div>;
export const getServerSideProps = () => {
return { redirect: { destination: "/", permanent: false } };
};
export default Custom404; We rewrite when nothing can be matched in the file system. The rewrite destination is a page that always redirects people back to No |
Beta Was this translation helpful? Give feedback.
-
You can also create a page called
And that is it :) |
Beta Was this translation helpful? Give feedback.
-
You can create a 404.js file in page directory. and write the following code ` import { useEffect } from "react" export default function 404Page() { useEffect(() => { return null |
Beta Was this translation helpful? Give feedback.
-
This answer right here actually solves the issue which also helps the robot crawlers SEO wise for a temporary redirection 302 and returns something valuable instead of a broken page that you wouldn't need to be indexed. in your import App, { AppContext } from 'next/app'
import type { AppProps } from 'next/app'
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
MyApp.getInitialProps = async (appContext: AppContext) => {
// calls page's `getInitialProps` and fills `appProps.pageProps`
const appProps = await App.getInitialProps(appContext);
if (appContext.ctx.res?.statusCode === 404) {
appContext.ctx.res.writeHead(302, { Location: '/' });
appContext.ctx.res.end();
}
return { ...appProps };
};
ecport default MyApp |
Beta Was this translation helpful? Give feedback.
-
Spent quite a while looking at alternatives to this, my preference was to use the redirects function within next.config.js to basically just reroute to home on a 404 status code. Unfortunately, you can't access the statusCode in headers on the Just thought I'd give people a heads-up if you ever end up down the rabbit hole. I think re-routing client-side is a disappointing alternative. |
Beta Was this translation helpful? Give feedback.
-
This solution should work fine. The page is Server side rendered too.
|
Beta Was this translation helpful? Give feedback.
-
For those that not found an useful answer here, I found one that redirects all pages to module.exports = {
async redirects() {
return [
{
source: '/:path(.{1,})', // this will redirect any other paths to `/`
destination: '/',
permanent: true
},
{
source: '/404',
destination: '/',
permanent: true
}
]
},
}; |
Beta Was this translation helpful? Give feedback.
-
I'd rather return this as an actual "use client";
import { useEffect } from "react";
export default () => {
useEffect(() => {
window.location.href = "/";
}, []);
return <div></div>;
}; |
Beta Was this translation helpful? Give feedback.
-
For me, here is a working option for server side component: not-found page: import {redirect} from "next/navigation" export default function Page() {
} |
Beta Was this translation helpful? Give feedback.
-
Solution without build errors:
import Index from "./index"
export default Index |
Beta Was this translation helpful? Give feedback.
-
This solution should work fine. import {redirect} from "next/navigation";
export default function NotFoundPage(){
redirect("/");
} |
Beta Was this translation helpful? Give feedback.
-
For me a reliable solution which works in development localhost and production server was following (app router): 1. RoutingUse following folder structure: app ├── (general)
│ ├── (home)
│ │ └── page.tsx
│ ├── whatever
│ │ └── page.tsx
│ └── layout.tsx
│
├── [...not-found]
│ ├── layout.tsx
│ └── page.tsx
│
├── globals.css
└── ...
2.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm using Nextjs and when I type any wrong route it'll navigate me to 404 page ( which is amazing feature ) but, what if I don't want it to navigate to 404 instead I want to navigate to homepage whenever user types any wrong route?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions