Safely Remove Dates from WordPress Permalinks

I’m not sure if this is a growing trend, but I recently decided to drop the dates from my blog post URLs. Previously I was using the day and name format /2017/12/12/identifying-bottleneck-plugins/ to the post name format /identifying-bottleneck-plugins/. This was not an SEO-based decision. I simply like the clean look in the URL, and feel that adding dates in the URLs adds unnecessary bloat considering the date is clearly shown on the posts.

Updating permlinks is fairly straightforward with WordPress.

Under Settings menu select Permalinks then pick your new format.

Redirecting old urls with a pattern match.

WordPress will attempt to redirect 404 pages with some basic matching, however I wanted to make sure all of my old URLs were covered. Luckily the Redirection plugin supports pattern matching. That means all old URLs can be forwarded with a single custom 301 rule. A rule with the source URL configured with ^\/\d{4}\/\d{2}\/\d{2}\/(.*) and the target URL configured to /$1 will match the URL format of /year/month/day/post-name/ and redirect to /post-name/.

Updating old URLs to new format

At this point the date formatted URLs will be redirected to the new post name format, however there are still lots of old URLs referenced throughout the website. In order to update those, we can do some regex magic with WP-CLI. Regex search and replaces can take a while to complete. Depending on the size of your site and speed of your web server it could easily take a few minutes or longer to complete. Just be patient and wait for it to finish.

wp search-replace 'anchor.host/\d{4}\/\d{2}\/\d{2}\/' 'anchor.host/' --dry-run --regex --report-changed-only --log

The format of this command is quite particular so be sure to add --dry-run and --log in order verify it’s going to do what you want. 

Once ready to do the final conversion simply remove those flags like so.

Confirm everything is working using Redirection logs.

The built-in logs with the Redirection plugin is great way to spot any issues with custom redirections rules. If everything is working you should see the following.