reply.redirect()

Sends an HTTP redirect to the given location.

Since Fastify 5 Spec ↗

Syntax

reply.redirect(url: string, code?: number)

Parameters

NameTypeRequiredDescription
url string No The target location.
code number No Optional status code, defaulting to 302.

Returns

FastifyReply — The reply, for chaining.

Examples

import Fastify from 'fastify';

const app = Fastify();

app.get('/old', async (_req, reply) => {
  return reply.redirect('/new', 301);
});

Notes

In Fastify 5 the signature is redirect(url, code?) with 302 as the default. Use 301 for permanent moves and 307/308 to preserve the request method.