Caddy Redirects
I decided that I wanted to move my fitness related stuff into the wiki folder. The only reason really was to keep the public_html directory cleaner and better organized.
I wanted to set up a quick 301 redirect in Caddy so that any links to the old stuff will get redirected to the new URL instead of just a 404.
I could have and did set up individual redirects at first but I got to thinking that there has to be a better way that is cleaner and could be useful in the future if I move things around. I knew I needed to use a request matcher to do that. When looking into it I saw the perfect solution. Caddy has a .startsWith option which is exactly what I needed since they all started with “fitness” in the file path.
@fitness `{path}.startsWith("/fitness/")`
Now that the matcher is set up I could go ahead and do the redirect. That was easy enough since the path is the same except for the “/wiki” added at the beginning.
redir @fitness /wiki/{path} permanent
It worked like a charm and gave me another way to look at it rather than individual redirects or trying to play with regex. This option is just more readable to me at a quick glance than some of the other solutions would have been..
It also works with matching regex. For example I moved /tags/ under into /blog/labels because I thought it made more sense for organization. Since I wanted to take part of the path name and changed it I needed to do it differently than before.
First I created a named matcher to match what I wanted to redirect:
@tags path_regexp tags ^/tags/(.*)$
Next I set up the redirect using that matcher
redir @tags /blog/labels/{re.tags.1}} permanent