Caddy Redirects

I’ve been wanting to remove the dates from my URLs for awhile now. It was easy enough to change the way Hugo generates the files but it was a bit more challenging to get Caddy server to do it. Once I figured it out I had the duh! moment though. I was forgetting a (-) because my bad eyes just weren’t seeing that it was missing but my brain was placing it there. I really haven’t used this feature of Caddy server up until now because I haven’t had the need for it.

I wanted to change the URL from:

/posts/2021-04-28-caddy-redirects/

to

/blog/caddy-redirects/

I had no other reason do this other than because I think it just looks a little cleaner. I know it’s really just bike shedding but it was fun to learn something new. I’m not great with regex because I really don’t use them that often. It’s really such a powerful tool though. After a little trial and error I got it sorted out.

@olddated path_regexp olddated ^\/posts\/([0-9]{4})(-)([0-9]{2})(-)([0-9]{2})(-)(.*)

redir @olddated /blog/{re.olddated.7} permanent

Sure I could have just let it be and not worry at all about the 404 errors but I wanted to learn something new. I certainly didn’t want to take my fifteen years worth of posts totaling around 850 currently and do individual redirects. I knew there had to be a way and I wanted the challenge of figuring it out.

Google was just letting me down yet again by showing me stuff that it thinks I want to see that is in actuality unrelated to what I’m searching for. For Nginx I could have found a copy and paste solution because that’s a much more popular server but I like the set it and forget about it Letsencrypt of Caddy plus I like using less common things. Who knows, maybe this will show up in google someday and help someone else trying to solve this or a similar problem.