{"slug":"astrov3-redirects","title":"利用Astrov3的原生重定向来实现各种各样的高级重定向！","description":"曾经，我使用Cloudflare Pages Redirects来实现我的个人短链重定向，而现在我发现我可以直接将它集成到我的Astro博客","coverImage":"https://b.acofork.com/img/astrov3-redirects-historical-cover.webp","coverWidth":795,"coverHeight":774,"date":"2025-09-01","tags":["开发"],"pinned":false,"wordCount":297,"html":"<h2 id=\"正式开始\">正式开始</h2><blockquote>\n<p>Astro v3 正式支持了原生的重定向 <a href=\"https://docs.astro.js.cn/en/guides/routing/#configured-redirects\" target=\"_blank\" rel=\"noopener noreferrer\">路由 | 文档 - Astro 文档</a></p>\n</blockquote>\n<p>仅需在 <code>astro.config.mjs</code> 中添加如下代码，示例代码将 <code>/tit</code> 的请求 302 重定向到 <code>/posts/pin</code> 。可以配置多行重定向规则</p>\n<div class=\"markdown-code\" data-language=\"js\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>js</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-js\"><span class=\"hljs-keyword\">import</span> { defineConfig } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">\"astro/config\"</span>;\n\n<span class=\"hljs-keyword\">export</span> <span class=\"hljs-keyword\">default</span> <span class=\"hljs-title function_\">defineConfig</span>({\n  <span class=\"hljs-attr\">redirects</span>: {\n    <span class=\"hljs-string\">\"/tit\"</span>: {\n        <span class=\"hljs-attr\">destination</span>: <span class=\"hljs-string\">\"/posts/pin/\"</span>,\n        <span class=\"hljs-attr\">status</span>: <span class=\"hljs-number\">302</span>,\n    },\n  }\n});</code></pre></div><p>有的小伙伴就会问了，如果我的Astro输出模式为SSG？那Astro的重定向是不是不支持 <code>location</code> 重定向？仅支持 <code>HTML</code> 重定向？</p>\n<p>的确，在不对构建服务商进行额外配置的情况下，Astro会使用兼容模式，创建 <code>HTML</code> 重定向，你可以尝试安装适配器来支持重定向，但需要注意 <strong>并不是所有适配器都会透传Astro中设置的重定向规则</strong> ，始终建议使用您构建服务商提供的重定向服务，参见： <a href=\"/posts/vercel-redirects/\">配置 Vercel.json 以支持服务器级别的重定向</a>。如配置 <code>vercel.json</code> 。关于 Astro 适配器的更多信息，参见 <a href=\"https://docs.astro.build/zh-cn/reference/configuration-reference/#adapter\" target=\"_blank\" rel=\"noopener noreferrer\">配置参考 | Docs</a></p>\n","toc":[{"id":"正式开始","text":"正式开始","depth":2}]}