Migrating rewrite/redir from v1 to v2

This is a follow-on from this post. I’ve migrated from v1 to v2 except for this rewrite:

example.blog {
  redir /feed https://blog.example.com/feed.xml 301
  rewrite / {
    r ^/(\d+){3}/(.*)	
    to {2}
   }
   redir / https://search.example.com/search?query=site%3Ablog.example.com+{2}
 }

The redir /feed works fine. It’s the rewrite which fails. The error from caddy v2 is:

run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': Caddyfile:46 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'

I’ve tried to do it in direct redir and uri but they both fail because I think it should parse the old html file name from the old /YYYY/MM/DD/blog-post.html uri structure. I’ve also tried rewrite * rather than rewrite /.

Suggestions? Pointers?

Thank you.

It works totally differently in v2.

First, read about request matchers:

In v1, matching was implemented differently for each directive. In v2, it’s now a generalized concept.

You need a path_regexp matcher for this.

@foo path_regexp foo ^/(\d+){3}/(.*)
rewrite @foo /{re.foo.2}

I just named the matcher and the regexp foo here, because I don’t know exactly the purpose of the rewrite, but I’m sure you can come up with something better.

Don’t forget to read the upgrade guide as well:

1 Like

@francislavoie Thank you for the pointers. After more reading, here’s what works:

example.blog {
  redir /feed https://blog.example.com/feed.xml 301
  @oldposts path_regexp oldpath ^/(\d+){4}/(\d+){2}/(\d+){2}/(.*)
  redir @oldposts https://search.example.com/search?query=site%3Ablog.example.com+{re.oldpath.4}
}

All 43 sites are now migrated to v2 in one Caddyfile. that redir/path_regexp was the final bit.

Thanks!

2 Likes

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