#Redirect preserving URL tags

3 messages · Page 1 of 1 (latest)

orchid maple
#

I have a page /work that I want to redirect to the homepage /, but keep all the URL tags. How can I do that?

viral thornBOT
#

Hi @orchid maple. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro’s routing docs don’t show a built‑in way to “redirect and preserve query params” in a single config entry, but you can achieve the behavior you want with a rewrite instead of a redirect.

A rewrite serves different content while keeping the original URL (including its query string) in the browser bar. So /work?tag=a&tag=b can show the same content as / without changing the URL. [Rewrites guide]

Create src/pages/work.astro:

---
const url = new URL(Astro.request.url);
// Build a URL pointing at "/", but keep the original search params
const target = new URL("/", url);
target.search = url.search;

return Astro.rewrite(target);
---

This:

  • Keeps /work?... in the address bar.
  • Renders the homepage (/) content, with all original query parameters preserved and passed to that page. [rewrite() API]

If you truly need a redirect (changing the URL to /), the docs only show static mappings like "/old-page": "/new-page" and don’t document a way to automatically copy query parameters, so I can’t provide a supported pattern for that from the sources. [Routing redirects]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: