V2: howto simple redir in Caddyfile?

1. My Caddy version (caddy -version):

v2.0.0-beta9 h1:oILdAOfunJ4ijBN9kOWjFIeH8EufBX/N1pC9HbnwjzU=

2. How I run Caddy:

a. System environment:

Ubuntu 19.10 eoan (GNU/Linux 5.3.0-22-generic x86_64)
systemd 242 (242)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

b. Command:

/usr/local/bin/caddy run --config /etc/caddy/Caddyfile --adapter caddyfile

c. Service/unit/compose file:

Here’s my caddy.service file for systemd:

[Unit]
Description=Caddy v2 web server
Documentation=https://github.com/caddyserver/caddy/wiki/v2:-Documentation
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]

; User and group the process will run as.
User=www-data
Group=www-data

; Always set "-root" to something safe in case it gets forgotten in the Caddyfile.
ExecStart=/usr/local/bin/caddy run --config /etc/caddy/Caddyfile --adapter caddyfile
ExecReload=/usr/local/bin/caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile

; Use graceful shutdown with a reasonable timeout
ExecStop=/usr/local/bin/caddy stop
KillMode=mixed
KillSignal=SIGQUIT
TimeoutStopSec=5s

; Limit the number of file descriptors; see `man systemd.exec` for more limit settings.
LimitNOFILE=1048576
; Unmodified caddy is not expected to use more than that.
LimitNPROC=4096

; The following additional security directives only work with systemd v229 or later.
; They further restrict privileges that can be gained by caddy. Uncomment if you like.
; Note that you may have to add capabilities required by any plugins in use.
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile:

{
	storage file_system {
		root	/etc/caddy/whatever
	}
	experimental_http3
}

sunrisemovement.dev, www.sunrisemovement.dev
root * /srv/sunrisemovement.dev/public/
try_files {path}.html {path}
encode gzip zstd brotli
file_server

3. The problem I’m having:

I just don’t know how to do a simple redirect from https://sunrisemovement.dev to https://www.sunrisemovement.dev in the V2 Caddyfile. Any functioning redir examples would help me out a lot. I tried a bunch of ideas but I’m just flailing in the dark, Caddy has refused to load all of my attempted Caddyfiles (I’m sure they were all terribly wrong). The one pasted above is one that works, but it obviously doesn’t do the redirect I want. A v1 Caddyfile would have written it like this:

redir 301 {
    if {host} starts_with sunrisemovement.dev
    / https://www.sunrisemovement.dev{uri}
  }

Help?

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

Thanks for trying v2 while it’s still in beta!

This is the syntax:

redir [<matcher>] to [<code>|permanent|temporary]

Have you tried:

redir https://www.sunrisemovement.dev

?

1 Like

Ohhhh now I understand the syntax slightly better! That did indeed work, with the following Caddyfile:

{
	storage file_system {
		root	/etc/caddy/whatever
	}
	experimental_http3
}

# test page
www.sunrisemovement.dev {
	root * /srv/sunrisemovement.dev/public/
	try_files {path}.html {path}
	encode gzip zstd brotli
	file_server
}

sunrisemovement.dev {
	redir https://www.sunrisemovement.dev
}

Thank you!

1 Like

That’s a good-looking Caddyfile right there! Have fun.