0
0
NextJSframework~10 mins

Redirect and rewrite in middleware in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Redirect and rewrite in middleware
Request comes in
Middleware runs
Redirect
New URL
Browser
When a request arrives, middleware checks it. It can redirect the browser to a new URL or rewrite the request to serve different content without changing the URL.
Execution Sample
NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  if (request.nextUrl.pathname === '/old') {
    return NextResponse.redirect(new URL('/new', request.url));
  }
  if (request.nextUrl.pathname === '/rewrite') {
    return NextResponse.rewrite(new URL('/api/data', request.url));
  }
}
Middleware redirects '/old' to '/new' and rewrites '/rewrite' to serve '/api/data' content.
Execution Table
StepRequest PathCondition CheckedAction TakenResulting URLBrowser URL
1/oldIs path '/old'?Yes - Redirect/new/new
2/newIs path '/old'?NoNo action/new
3/rewriteIs path '/old'?NoCheck next condition/rewrite
4/rewriteIs path '/rewrite'?Yes - Rewrite/api/data/rewrite
💡 Middleware stops after redirect or rewrite action; browser URL changes only on redirect.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
request.nextUrl.pathname/old/old/rewrite/rewrite/rewrite
actionnoneredirect to /newnonerewrite to /api/datarewrite to /api/data
browser URL/old/new/rewrite/rewrite/rewrite
Key Moments - 2 Insights
Why does the browser URL change after a redirect but not after a rewrite?
Redirect sends a response telling the browser to load a new URL, so the browser URL updates (see execution_table step 1). Rewrite changes the server's internal request path without telling the browser, so the browser URL stays the same (see step 4).
What happens if multiple conditions match in middleware?
Middleware returns immediately after the first redirect or rewrite. Later conditions are not checked (see execution_table steps 1 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the browser URL after step 1?
A/rewrite
B/old
C/new
D/api/data
💡 Hint
Check the 'Browser URL' column in execution_table row for step 1.
At which step does the middleware perform a rewrite?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Action Taken' column for rewrite action in execution_table.
If the request path was '/old' and the redirect was removed, what would happen at step 1?
ARedirect to /new
BNo action, continue processing
CRewrite to /api/data
DError thrown
💡 Hint
Refer to execution_table step 1 and the conditions checked.
Concept Snapshot
Middleware intercepts requests in Next.js.
Use NextResponse.redirect() to send browser to a new URL.
Use NextResponse.rewrite() to serve different content without changing browser URL.
Redirect changes browser URL; rewrite does not.
Middleware stops after first redirect or rewrite.
Useful for URL changes and content routing.
Full Transcript
In Next.js middleware, when a request arrives, the middleware function runs first. It checks the request path. If the path is '/old', it redirects the browser to '/new', changing the browser's URL. If the path is '/rewrite', it rewrites the request internally to '/api/data' without changing the browser URL. The middleware returns immediately after redirect or rewrite, so only one action happens per request. Redirect tells the browser to load a new page, while rewrite serves different content behind the scenes. This helps manage URLs and content dynamically before the page loads.