Changing Wordpress' URL structure

Changing Wordpress' URL structure

This site has public articles dating back to March 2009. At some point in my blogging journey I moved to using WordPress as a platform and inherited a url structure with dates in it. I recently got rid of this structure and simplified it to be the name of the article.

If you had come to this site a month ago a typical blog post url would have been:

After removing the year and month, the url structure is now:

The url is now much easier to read and doesn't unfairly signal out of date content. (In the case of the quoted link the content won't ever get out of date).

Before you make a change like this you need to figure out how to make sure users accessing your old urls get pointed to the new ones. (Imagine someone going to an old link on a website or finding a link on Google.) You need to set up redirects from the old to the new urls. If you are lucky you won’t need many.

Redirection with RegEx

To limit the number of redirects I wanted a way to match all incoming requests for a given url structure like /2019/02/name-of-article and then redirect it to that same url minus the year and date. To do this I created a redirection rule using a regular expression (RegEx).

Source URL regular expression: /^([0-9]{4})/([0-9]{2})/(.*)

This source url regular expression says if any of the first 4 values in the url are numbers of 0-9 and if the second 2 values are numbers of 0-9, then take whatever is passed in as the third value and send it to the destination url. This removes the year and day from my url structure.

Destination URL:$3

The value passed by the regular expression is then append to source destination in place of the $3

There we go. Now most (all?) of my traffic should be redirected to the correct structure.

Finally we need to change the url structure. In WordPress this is called changing the permalink structure. Turns out it's incredibly easy to do.

I went from the Day and Name option to the Post name option.

When setting redirects or even permalinks, it’s straightforward to make a change and then test it to see if it’s working. Be sure to take a backup prior, just in case.

Subscribe to Shattered Illusion by Chris Kenst

Sign up now to get access to the library of members-only issues.
Jamie Larson