Redirect www domain with Caddy

Redirect www domain with Caddy

Here's a quick snippet if you want to use Caddy and want to redirect www domains (such as www.example.com) to the "base" example.com.

For this example I will assume you have a current Caddyfile with the below configuration:

example.com {
	<Your Site Configuration>
}

Now update the Caddyfile to include the redirect. Note: the www redirect must be above your site's config to actually redirect.

www.example.com {
	redir https://example.com{uri}
}

example.com {
	<Your Site Configuration>
}

Once the Caddyfile is updated, reload Caddy and access your site with the www domain to see it redirect to the www removed address.

There is a lot more you can do with Caddy. The best place to start is the official docs which has a great list of examples for the most common use cases.