vercel / vercel/next.js

NextResponse.redirect() behaves incorrectly when redirecting from subdomain route to apex domain route

Open
#75,173 8 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/bentron2000/next-response-bug-report

To Reproduce

Set /etc/hosts:

127.0.0.1 example.test
127.0.0.1 foo.example.test

Install Packages:

npm i

Start Server:

npm run dev

Then Navigate to the following link and observe the result.
'http://foo.example.test:3000/shouldredirect'

Current vs. Expected behavior

When you navigate to the 'http://foo.example.test:3000/shouldredirect'
Middleware should select for this route and redirect to the apex domain '/login' page

Expected Result:
Redirect to : 'http://example.test:3000/login' -> 'Hooray - we redirected properly'

Actual Result:
Redirect to : 'http://foo.example.test:3000/login' -> 'Should not be redirected here'

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 18:51:28 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8112
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.10.0
  npm: 10.8.1
  Yarn: 1.22.22
  pnpm: 9.15.0
Relevant Packages:
  next: 15.2.0-canary.19 // Latest available version is detected (15.2.0-canary.19).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Middleware

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

next dev (local)

Additional context

Minimal demo of possible middleware NextResponse.redirect() Bug

Demonstrates that NextResponse.redirect is not obeying explicit urls when redirecting from a subdomain foo.example.test to the apex domain example.test

In this example, middleware has been set to catch a specific subdomain route and to redirect it to an explicitly set route on the apex domain
'http://foo.example.test:3000/shouldredirect' -> 'http://example.test:3000/login'

This does not happen - it redirects to the same path but on the subdomain.

To Reproduce

Set /etc/hosts:

127.0.0.1 example.test
127.0.0.1 foo.example.test

Install Packages:

npm i

Start Server:

npm run dev

Then Navigate to the following link and observe the result.
'http://foo.example.test:3000/shouldredirect'

Middleware should select for this route and redirect to the apex domain '/login' page

Expected Result:
Redirect to : 'http://example.test:3000/login' -> 'Hooray - we redirected properly'

Actual Result:
Redirect to : 'http://foo.example.test:3000/login' -> 'Should not be redirected here'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked next-response-bug-report reproduction: configure /etc/hosts, run npm i and npm run dev, then visit /shouldredirect on foo.example.test:3000. Investigate the Middleware behavior around NextResponse.redirect(); done means the request reaches http://example.test:3000/login instead of the subdomain URL.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, react
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.