User Guide

http.redir

redir sends the client an HTTP redirect status code if the URL matches the specified pattern. It is also possible to make a redirect conditional.

Syntax

redir from to [code]

To create a permanent, "catch-all" redirect, omit the from value:

redir to

If you have a lot of redirects, share a redirect code by making a table:

redir [code] { from to [code] }

Each line defines a redirect and may optionally overwrite the redirect code defined at the top of the table. If no redirect code is specified, the default is used.

A group of redirects can also be conditional:

redir [code] { if a cond b if_op [and|or] ... }

Preserving Path

By default, redirects are from precisely matching paths to the precise location you've defined. You can preserve the path or other portions of the request URL by using replaceable values, such as {uri} or {path}, in any "to" argument. Only request placeholders are supported.

Examples

When a request comes in for /resources/images/photo.jpg, redirect to /resources/images/drawing.jpg with HTTP 307 (Temporary Redirect) status code:

redir /resources/images/photo.jpg /resources/images/drawing.jpg 307

Redirect all requests to https://newsite.com while preserving the request URI:

redir https://newsite.com{uri}

Defining multiple redirections that share a 307 status code, except the last one:

redir 307 { /foo /info/foo /todo /notes /api-dev /api meta }

Redirect only if the forwarded protocol is HTTP:

redir 301 { if {>X-Forwarded-Proto} is http / https://{host}{uri} }

Need help with this feature?

Join us in the Caddy forum, where the open source community gathers to share their knowledge.

Does your company use Caddy?

Purchase a commercial license to use Caddy with your business and for basic email support, along with other benefits!