Skip to content
← All solutions

Astro / Routing

Astro redirect data does nothing until a route serves it

Turn a redirect table into an HTTP response, preserve the query string, and keep unknown paths at 404.

Applies to

Astro 4, server output, Vercel serverless adapter. The example uses standard Request and Response APIs available in Node.js 20.

The symptom

A legacy URL returns 404 even though a redirect table maps it to the replacement page.

Why it happened

The application had moved /journal and /journals into a JSON redirect table and removed their page files. The table was parsed, but no request handler used it. Configuration alone did not create an HTTP route.

The fix

Add a catch-all endpoint at src/pages/[...path].ts. Look up the exact pathname, return the configured redirect, and return 404 when there is no match. Keep this endpoint server-rendered so it can handle arbitrary incoming paths.

// src/pages/[...path].ts
// getModeledRedirect reads the application-owned redirect table.
import { getModeledRedirect } from '@/lib/site-data';

export const prerender = false;

export function GET({ request }: { request: Request }) {
  const url = new URL(request.url);
  const redirect = getModeledRedirect(url.pathname, url.search);
  if (redirect) {
    return Response.redirect(new URL(redirect.location, url), redirect.status);
  }
  return new Response('Not found', {
    status: 404,
    headers: { 'content-type': 'text/plain; charset=utf-8' },
  });
}

Check the result

  1. Run the standalone reproduction linked below with Node.js 20 or newer. It asserts that the disconnected version returns 404, while the connected version returns 301 and the expected Location header.
  2. With the application running, request /journal?from=bookmark without following redirects. Expect 301 and a Location ending in /writing?from=bookmark.
  3. Request a path absent from the table. Expect 404, not a redirect to the home page.

Where this stops applying

This is an exact-path, application-owned redirect table. It does not implement wildcard rules, trailing-slash normalization or redirects supplied by visitors. Validate destinations before accepting untrusted redirect input. Other Astro rendering modes or adapters need their own deployment check.

Source and evidence

The request handler was added after d104272 moved the aliases into redirect data. The excerpt above retains that handler’s behavior.

Source file: apps/portfolio/src/pages/[...path].ts

Revision: 1f4040a686d838c5fd0a484166212df497f67aba

The source repository is private. The public reproduction lets you inspect and test the reduced behavior without repository access. A source revision records a change; it does not prove compatibility with your application.

The public Node example checks reduced Request/Response wiring. It does not reproduce Astro route discovery or Vercel deployment behavior.

Browse all fixes