---
title: "How do I redirect in Next.js?"
description: "Next.js has four main ways to redirect: redirect() and permanentRedirect() in your code, redirects in next.config, and proxy.ts. Here is which one to reach for and why."
url: https://www.danny.engineering/answer/nextjs/how-do-i-redirect-in-nextjs
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Next.js"]
---

# How do I redirect in Next.js?

## The short answer


Inside a Server Component, Server Action or Route Handler, call `redirect()` from `next/navigation`. For fixed URL changes that never depend on data, such as an old blog path, add a rule to `redirects` in `next.config.ts`. For checks that must run before any page renders, such as a signed-in cookie, use `proxy.ts`.


```typescript
import { redirect } from 'next/navigation';

export default async function Page() {
  const user = await getUser();
  if (!user) redirect('/login');

  return <h1>Welcome back, {user.name}</h1>;
}
```


## Why it happens


There is no single redirect API because redirects happen at different moments in a request.

- `next.config` redirects are checked first, before the request reaches your app. They suit permanent moves.
- `proxy.ts` (called `middleware.ts` before Next.js 16) runs next, for every matching request, and can read cookies and headers.
- `redirect()` and `permanentRedirect()` run while a route is rendering or while a Server Action or Route Handler runs, so they can use data you have just loaded.
- `useRouter` runs in the browser, after the page has loaded, in response to user events.

Picking the earliest stage that has the information you need keeps redirects fast.


## How to fix it


### redirect() and permanentRedirect() in your code


`redirect()` sends a 307 temporary redirect. `permanentRedirect()` sends a 308, which tells search engines the move is permanent. Use it when a slug changes.


```typescript
// app/blog/[slug]/page.tsx
import { notFound, permanentRedirect } from 'next/navigation';

export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPostBySlugOrAlias(slug);

  if (!post) notFound();
  if (post.slug !== slug) permanentRedirect(`/blog/${post.slug}`);

  return <article>{post.title}</article>;
}
```


In a Server Action, `redirect()` sends the user on after a form submits. With JavaScript running it does a client-side navigation; for a plain form post it sends a 303 so the browser follows with a GET.


```typescript
'use server';

import { redirect } from 'next/navigation';

export async function createPost(formData: FormData) {
  const post = await db.post.create({ data: { title: String(formData.get('title')) } });
  redirect(`/blog/${post.slug}`);
}
```


### Fixed rules in next.config


For URL changes known ahead of time, add them to `next.config.ts`. `permanent: true` sends a 308 and `permanent: false` sends a 307. Path parameters such as `:slug` carry over.


```typescript
// next.config.ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  async redirects() {
    return [
      { source: '/old-blog/:slug', destination: '/blog/:slug', permanent: true },
      { source: '/docs', destination: '/guides', permanent: false },
    ];
  },
};

export default nextConfig;
```


### Request checks in proxy.ts


When the decision depends on the request but not on page data, redirect in `proxy.ts` at the project root. Use `matcher` so it only runs where needed.


```typescript
// proxy.ts
import { NextResponse, type NextRequest } from 'next/server';

export function proxy(request: NextRequest) {
  if (!request.cookies.has('session')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
}

export const config = {
  matcher: ['/dashboard/:path*'],
};
```


A cookie check here is an early, optimistic check. Still verify the session where you read the data.


### Redirect after a click


`redirect()` cannot be called from an event handler. In a Client Component, use the router instead.


```typescript
'use client';

import { useRouter } from 'next/navigation';

export function DoneButton() {
  const router = useRouter();
  return <button onClick={() => router.push('/dashboard')}>Done</button>;
}
```


## Common pitfalls

- **Calling redirect inside try.** `redirect()` works by throwing a special error. If you catch it, the redirect never happens. Call it after the `try` block.
- **Writing return redirect().** You do not need `return`; the function never returns. TypeScript knows this because its return type is `never`.
- **Importing from next/router.** That is the Pages Router. In the App Router use `next/navigation`.
- **Using permanent redirects for temporary moves.** Browsers cache 308s hard. Use 307 while you are unsure.
- **Redirect loops.** A proxy that sends everyone without a cookie to `/login` must not match `/login` itself. Check your `matcher`.
- **Still using middleware.ts.** In Next.js 16 the file and exported function are named `proxy`. The old name is deprecated.

## Related reading

- [Next.js: Redirecting guide](https://nextjs.org/docs/app/guides/redirecting)
- [Next.js: redirect](https://nextjs.org/docs/app/api-reference/functions/redirect)
- [Next.js: redirects in next.config](https://nextjs.org/docs/app/api-reference/config/next-config-js/redirects)
