Redirect to date-less permalinks on WordPress with Caddy.

Motivation

I was advised, long-ago, that having dates in permalinks help a search engine determine how fresh the page is. With certain meta tags, my guess is they are no longer necessary.

And who doesn’t like clean links?

Goal

Say your permalinks are like so:

/2021/09/13/block-images-appearing-in-search/

And now you have decided to move to a date-less permalink, like so:

/block-images-appearing-in-search/

If you self-host WordPress and use Caddy, here’s what I did.

Configure Caddy.

I use a named regex path here (matcher). Then I ask any matches to be redirected (directive) to the fourth capture group in the regular expression. That is the post slug.

# Redirect to date-less URIs.
@wordpress_date_permalink path_regexp date_slug /([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)/
handle @wordpress_date_permalink {
        redir * /{http.regexp.date_slug.4}/
}

When I decide I no longer want to go back, I’ll make this a permanent redirect.

Configure WordPress.

Log in to your WordPress Admin panel, and update the permalink structure. From the sidebar, find Settings > Permalinks.

The Permalink Settings page.

Reload Caddy.

We have our server configured, WordPress updated. Now quickly reload Caddy, so it can start redirecting to prettier links. πŸ™‚

sudo systemctl reload caddy

Extra: Clear your local and/or CDN cache if you cache HTTP headers as well.

Note on SEO.

With WordPress, at least, I do see the following two meta tags on an article:

<meta property="article:published_time" content="2021-09-13T09:37:01+00:00" />
<meta property="article:modified_time" content="2021-09-13T09:38:54+00:00" />

Those are apparently Facebook’s Open Graph tags. My guess is we’re okay since it is also the most understood object data structure. πŸ™‚

Discuss on Fosstodon, Twitter, or write me an email.