{
  "id": "nextjs/middleware-redirect-to-external-url",
  "signature": "Error: Middleware redirected to an external URL. Redirects must be to relative paths or internal routes.",
  "signature_zh": "错误：中间件重定向到外部 URL。重定向必须是相对路径或内部路由。",
  "regex": "Middleware redirected to an external URL",
  "domain": "nextjs",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Next.js middleware restricts the NextResponse.redirect() method to only accept relative URLs or paths within the same application, preventing security issues from open redirects.",
  "root_cause_type": "generic",
  "root_cause_zh": "Next.js 中间件限制 NextResponse.redirect() 方法仅接受相对 URL 或同一应用程序内的路径，以防止开放重定向的安全问题。",
  "versions": [
    {
      "version": "13.5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "14.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "14.1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "15.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using window.location.href inside middleware to redirect externally",
      "why_fails": "Middleware runs on the server/edge runtime, not in the browser. window is not defined.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting the Location header manually in the middleware response",
      "why_fails": "Next.js middleware response headers are immutable for security; manual header manipulation is ignored or causes a different error.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Redirect to a relative path within your app and then handle the external redirect on the client side using useEffect or a Server Action. Example: export function middleware(request) { return NextResponse.redirect(new URL('/external-redirect', request.url)); } Then in the /external-redirect page, call window.location.href = 'https://example.com'.",
      "success_rate": 0.9,
      "how": "Redirect to a relative path within your app and then handle the external redirect on the client side using useEffect or a Server Action. Example: export function middleware(request) { return NextResponse.redirect(new URL('/external-redirect', request.url)); } Then in the /external-redirect page, call window.location.href = 'https://example.com'.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use NextResponse.next() and set a custom header to signal the client to redirect, then read that header in a layout or page component.",
      "success_rate": 0.85,
      "how": "Use NextResponse.next() and set a custom header to signal the client to redirect, then read that header in a layout or page component.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Redirect to a relative path within your app and then handle the external redirect on the client side using useEffect or a Server Action. Example: export function middleware(request) { return NextResponse.redirect(new URL('/external-redirect', request.url)); } Then in the /external-redirect page, call window.location.href = 'https://example.com'.",
    "Use NextResponse.next() and set a custom header to signal the client to redirect, then read that header in a layout or page component."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://nextjs.org/docs/app/building-your-application/routing/middleware#nextresponseredirect",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-10-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}