Redirects in Astro

Author:Lars Nyström
Updated:Jun 9, 2025

Redirects in Astro

You can configure redirects in your astro.config.ts file using the redirects option.

Redirects in Astro Config

import { defineConfig } from 'astro/config';

export default defineConfig({ redirects: { '/old-page': '/new-page', }, });


### Redirects in Server Endpoints

You can also return a redirect from a Server Endpoint using the `redirect` utility:


src/pages/links/[id].ts
export async function GET({ params, redirect }: APIRoute) {
  return redirect('/redirect-path', 307);
}

Redirects in Components

You can redirect from within Astro components with Astro.redirect():

src/pages/account.astro
import { isLoggedIn } from '../utils';

const cookie = Astro.request.headers.get('cookie');

if (!isLoggedIn(cookie)) {
  return Astro.redirect('/login');
}

<h1>You can only see this page while logged in</h1>

Astro Middleware on Esadax

Executes before a request is processed on a site, allowing you to modify responses to user requests.

  • Runs on all requests, but can be scoped to specific paths through a matcher config.
  • Uses Esadax's lightweight Edge Runtime to keep costs low and responses fast.

Caching

Esadax automatically caches static files at the Edge after the first request, and stores them for up to 31 days on Esadax's Edge Network. Dynamic content can also be cached, and both dynamic and static caching behavior can be configured with Cache-Control headers.

Example: Serving Stale Content

The following Astro component will show a new time every 10 seconds. It does by setting a 10 second max age on the contents of the page, then serving stale content while new content is being rendered on the server when that age is exceeded.

src/pages/ssr-with-swr-caching.astro
Astro.response.headers.set('Cache-Control', 's-maxage=10, stale-while-revalidate');
const time = new Date().toLocaleTimeString();

<h1>{time}</h1>

CDN Cache-Control Headers

You can also control how the cache behaves on any CDNs you may be using outside of Esadax's Edge Network with CDN Cache-Control Headers.

src/pages/ssr-with-swr-caching.astro
Astro.response.headers.set('Esadax-CDN-Cache-Control', 'max-age=3600');
Astro.response.headers.set('CDN-Cache-Control', 'max-age=60');

const time = new Date().toLocaleTimeString();

<h1>{time}</h1>

Caching on Esadax

Automatically optimizes and caches assets for the best performance.

  • Requires no additional services to procure or set up.
  • Supports zero-downtime rollouts.