# 错误：服务器操作无法重定向到外部 URL。请改用 NextResponse.redirect()。

- **ID:** `nextjs/server-action-redirect-external`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

服务器操作使用 'use server' 指令，只能通过 redirect() 重定向到内部路由。外部 URL 需要不同的方法，例如在中间件中使用 NextResponse.redirect() 或客户端重定向。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@14.2.0 | active | — | — |
| next@15.0.0 | active | — | — |

## 解决方案

1. ```
   从服务器操作返回重定向 URL，并在客户端使用 useRouter 或 window.location 处理重定向。
   ```
2. ```
   在中间件中使用 NextResponse.redirect()，并通过服务器操作设置的自定义 header 触发。
   ```

## 无效尝试

- **** — Server Actions run on the server; window is not defined, causing a ReferenceError. (95% 失败率)
- **** — This adds complexity and may not work with Next.js error boundaries, leading to unhandled rejections. (70% 失败率)
