• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License

How to redirect to a page with Next.js

Jul 29, 2022

Learn the ways to redirect users to another page with Next.js

next

Depending on whether we are redirecting from an API endpoint or from a server-side rendering function, there are different ways to redirect users to another page using Next.js.

Redirecting from an API function in Next.js

If you are handling an API function, you can use the redirect method on the NextApiResponse object to redirect the user to another page, as seen below:

tsx
function myApiHandler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (notFound()) {
return res.redirect(HttpStatusCode.NotFound, `/404`);
}
}
export default myApiHandler;

Redirecting users before rendering the page in Next.js

Another common way to redirect users to another page is using the getServerSideProps function when rendering the page server-side with Next.js.

This function is particularly useful, for example, when checking the authentication state of the user before rendering the correct page.

To do so, we have to return the object containing redirect as a property from the gerServerSideProps function with the following interface:

tsx
{
redirect: {
permanent: boolean;
destination: string;
}
}

Let's see a complete example:

tsx
export default function ProtectedPage() {
return <></>;
}
export function getServerSideProps(ctx: GetServerSidePropsContext) {
if (isLoggedIn()) {
const props = getProps();
return {
props,
}
}
const destination = `/auth/sign-in?returnUrl=${returnUrl}`;
return {
redirect: {
permanent: false,
destination,
},
};
}

And that's it! You now know everything you need to be able to redirect users to other pages with Next.js!

Some other posts you might like...
Jun 9, 2025Claude Code: Build a SaaS with AIThis is a step-by-step guide to building an AI Content Repurposer SaaS by vibe-coding with Claude Code and Makerkit.
Apr 23, 2025Next.js Security: A Comprehensive Guide how to secure your Next.js applicationA comprehensive guide to securing your Next.js application, focusing on practical strategies to protect your application and user data from common vulnerabilities.
Jan 17, 2025Best Practices for Building a SaaS with Windsurf and MakerkitWindsurf is a new AI-powered editor taking the developer experience to the next level. With the new optimized rules for Makerkit, building a SaaS just got a lot easier!
Jan 16, 2025Best Practices for Building a SaaS with Cursor and MakerkitCursor is the hottest AI editor in the market. With the new optimized rules for Makerkit, building a SaaS just got a lot easier!
Dec 26, 2024Choosing the best hosting provider for your Next.js applicationIn this post, we'll show you how to choose the best hosting provider for your Next.js application.
Dec 24, 2024Next.js API Routes: The Ultimate GuideLearn how to build robust, secure, and efficient API endpoints in Next.js. This comprehensive guide will walk you through the critical best practices for creating robust, secure, and efficient API endpoints in Next.js.