nextjs - ✅(Solved) Fix When using `redirect` in Route Handler, it doesn't respect `baseUrl` [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#85267Fetched 2026-04-08 02:16:41
View on GitHub
Comments
0
Participants
1
Timeline
6
Reactions
1
Author
Participants
Timeline (top)
labeled ×3cross-referenced ×1issue_type_added ×1subscribed ×1

Fix Action

Fixed

PR fix notes

PR #85339: fix: prepend base path when redirecting

Description (problem / solution / changelog)

Fixes: https://github.com/vercel/next.js/issues/85267

Summary

Add basePath support to redirect functions

Changes

This PR modifies the redirect() and permanentRedirect() functions to automatically prepend the basePath (configured in next.config.js) to relative redirect URLs.

Implementation

  • Added getBasePath() function: Retrieves the basePath from process.env.__NEXT_ROUTER_BASEPATH
  • Added prependBasePath() helper: Safely combines basePath with redirect URLs while handling edge cases:
    • Skips absolute URLs (with http:// or https://)
    • Prevents duplicate basePath prepending
    • Handles trailing/leading slashes correctly
  • Modified getRedirectError(): Now prepends basePath to the URL before creating the redirect error

Changed files

  • packages/next/src/client/components/redirect.test.ts (modified, +165/-2)
  • packages/next/src/client/components/redirect.ts (modified, +31/-5)

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:45 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.11.0
Relevant Packages:
  next: 16.0.1-canary.0 // Latest available version is detected (16.0.1-canary.0).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/eestpavel/nextjs-routehandler-redirect

To Reproduce

Overall issue

  1. Set baseUrl in next.config
  2. Call redirect/permanentRedirect function with relative path in Route Handler
  3. Result: Location response header doesn't include baseUrl, but only relative path

Provided reproduction

  1. Check the baseUrl - it was set to /experiments

  2. Call Route Handler by sending request to localhost:3000/experiments/api/healthcheck, it will return 308 status code with Location header

    <img width="609" height="533" alt="Image" src="https://github.com/user-attachments/assets/a615a589-6c71-4ce6-812e-322b1223232c" />
  3. Next request goes to /destination, not /experiments/destination, resulting with 404 status code

    <img width="609" height="533" alt="Image" src="https://github.com/user-attachments/assets/3ec52983-f7f6-4f7e-a1d5-b5310e11cdce" />

Current vs. Expected behavior

Expected

Location response header contains correct redirect URL respecting baseUrl, i.e. /experiments/destination

Actual

Location response header doesn't contain baseUrl in redirect URL, i.e. /destination

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:45 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.11.0
Relevant Packages:
  next: 16.0.1-canary.0 // Latest available version is detected (16.0.1-canary.0).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Linking and Navigating, Redirects, Route Handlers

Which stage(s) are affected? (Select all that apply)

next dev (local), Other (Deployed), next start (local)

Additional context

Issue was originally found in version 15.5.2, then tested with the latest canary release

extent analysis

TL;DR

The most likely fix is to modify the redirect or permanentRedirect function in the Route Handler to include the baseUrl in the redirect URL.

Guidance

  • Verify that the baseUrl is correctly set in next.config and that it is being used in the Route Handler.
  • Check the implementation of the redirect and permanentRedirect functions to ensure they are correctly handling relative paths and including the baseUrl in the redirect URL.
  • Consider modifying the Route Handler to use absolute paths for redirects instead of relative paths.
  • Test the fix by sending a request to localhost:3000/experiments/api/healthcheck and verifying that the Location response header includes the correct redirect URL with the baseUrl.

Example

// Example of how to modify the redirect function to include baseUrl
const baseUrl = '/experiments';
const destination = '/destination';
const redirectUrl = new URL(destination, `${req.headers.host}${baseUrl}`).href;
return NextResponse.redirect(redirectUrl, 308);

Notes

The issue seems to be specific to the implementation of the Route Handler and how it handles redirects with relative paths. The provided example code and environment information suggest that the issue is not related to the Next.js version or configuration.

Recommendation

Apply workaround: Modify the redirect and permanentRedirect functions in the Route Handler to include the baseUrl in the redirect URL, as shown in the example above. This should fix the issue without requiring an upgrade to a different version of Next.js.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING