noteTb: 252
This data as json
| id | user_id | content | tags | created_at | updated_at | enable | pinned | folder_id | comment | position | visibility |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 252 | 1 1 | # Vercel 路径重定向经验 作者:Manus 版本:1.0 ## 需求 将 `www.imocfood.com/finance` 访问重定向到外部域名 `leedreamer.cn`。 ## 关键结论 **DNS 层面(Namecheap)无法做路径级别重定向。** DNS 只能处理域名,无法识别 `/finance` 这样的路径。 Namecheap 的 URL Redirect Record 只支持子域名级别(如 `finance.imocfood.com` → 某地址),不支持路径级别(`/finance`)。 ## 解决方案:在 vercel.json 配置 redirects 在项目根目录的 `vercel.json` 中添加 `redirects` 字段,放在 `rewrites` 之前: ```json { "buildCommand": "pnpm run build:vercel", "outputDirectory": "dist/public", "installCommand": "pnpm install --frozen-lockfile", "redirects": [ { "source": "/finance", "destination": "https://leedreamer.cn", "permanent": false } ], "rewrites": [ { "source": "/((?!api/).*)", "destination": "/index.html" } ] } ``` **参数说明:** - `source`:匹配的路径(相对于域名根) - `destination`:跳转目标,可以是外部完整 URL - `permanent: false`:302 临时重定向;`true` 为 301 永久重定向 ## 注意事项 - `redirects` 必须放在 `rewrites` 之前,否则 SPA 的通配符 rewrite 会先匹配 `/finance`,导致重定向失效 - Vercel 检测到 GitHub main 分支更新后自动触发重新部署,无需手动操作 - 本方案完全免费,无需修改 Namecheap DNS ## 部署步骤 1. 修改 `vercel.json`,添加 `redirects` 规则 2. `webdev_save_checkpoint` 保存检查点 3. 推送到 GitHub: ```bash git remote set-url github https://leedreamer4gmail:<TOKEN>@github.com/leedreamer4gmail/imocfood.git git fetch github && git merge github/main --no-edit git push github main git remote set-url github https://github.com/leedreamer4gmail/imocfood.git ``` 4. Vercel 自动重新部署后生效 ## 适用场景 - 将网站某个路径指向另一个域名(如个人主页、其他项目) - 旧路径迁移(301 永久重定向) - A/B 测试跳转 | [] | 2026-05-12 13:59:57 | 2026-05-12 13:59:57 | T | F | source=simplenote; source_id=753fa8ee-e818-4700-a000-9dcc4cd98a85 | 27 | public |