• 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:

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:

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

Let's see a complete example:

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...
Oct 22, 2025Next.js 16: what's new?Next.js 16 is a major release that includes several new features and improvements. In this article, we will cover the new features and improvements in Next.js 16.
Oct 13, 2025Build a Production Supabase Blog in Next.js with Supamode CMSLearn how to design a Supabase blog schema, build a Next.js UI, and wire up Supamode CMS so your team can publish fast without touching SQL
Sep 25, 2025Mastering AI-Driven Development: Claude Code & Makerkit Best PracticesA comprehensive guide to building production-ready SaaS features using Claude Code's intelligent agents and Makerkit's PRD functionality
Jun 9, 2025Build a SaaS with Claude CodeThis 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!