Caddy v1 regexp path to redir new domain?

I changed static file generators and domain names. The old site format was old.domain/YYYY/MM/DD/title-here. The new site format is new.domain/title-here. I’m trying to use a regexp to grab the “title-here” part and redir to the new url.

in pseudo code, this is what I want:
request comes in for https://old.domain/2020/06/14/sample-title-here
caddy rewrites and redirects to

Here’s what I have, but doesn’t work:

old.domain {
  rewrite {
    r ^/(\d+)/(\d+)/(\d+)/(.*)

I’m assuming {4} matches the (.*) which is what I want.

Failed attempts

  1. I’ve tried a straight regexp to redir. like this
rewrite {
  r ^/(\d+)/(\d+)/(\d+)/(.*)
  to redir{4}
  1. I’ve tried the stanza above and have it call {1}
  2. And I’ve tried straight redir to no avail.

Thanks for the help in advance.

You’re looking for the redir directive:

FYI, Caddy v1 is no longer under active maintenance, you should consider moving to Caddy v2.

Thanks. I started with redir alone, but couldn’t get redir to handle path rewriting via regexp.

migrating to v2 is on my to do list once v2.1 is released.

Earlier today, I started with this.

I’m still missing something around regex:

redir 302 {
  if {path} match ^/(\d+)/(\d+)/(\d+)/(.*)

This is silly because everything at the old domain will match that regex, so i could just as easily do:

redir /{path} 302

Except, that’s not going to work because the search engine is looking for the {4} equivalent from


After lots of experimentation, I arrive at this:

old.domain {
  redir /feed https://new.domain/feed.xml 301
  rewrite / {
        r ^/(\d+)/(\d+)/(\d+)/(.*)
        to {4}
  redir /{4}

It works, it’s not fast at all, but it’s working.


I look forward to re-writing my config with v2.1 soon.

This topic was automatically closed after 30 days. New replies are no longer allowed.