Are you looking to change the permalink url structure of posts on your WordPress website? We had a client website with an older blog that utilized the day and name permalink structure which includes the specific year, month, and day within the post url structure. As an example you could have a post located at domain.com/2023/02/21/name-of-post. This is all great and dandy, but what if you wanted to switch all of these over to a cleaner url without the date information such as domain.com/blog/name-of-post? You could easily go into your WordPress permalink settings and switch over to a custom structure, but this will break all of your old URLs established in search engines. Not good!
There are a variety of ways to handle this depending on your specific setup and comfort level. There are also some useful plugins that can help with this such as “Simple 301 Redirects” and “Redirection”. Both of those plugins are great, but we try to have the philosphy of minimizing plugins on WordPress powered websites. One less thing to load, one less thing to update, and one less thing to potentially cause a security or compatibility issue. On top of this, redirects are always best handled at the server level for the fastest execution.
In this specific example, our client was hosted on WP-Engine, one of our favorite web hosts for WordPress websites. One of the benefits of this platform is the unique dashboard they provide which has a section specifically dedicated to the management of redirects. Their redirect section also allows for “REGEX” to be utilized which is what we will be using in this example for creating a bulk redirect. This method is not specific to WP-Engine, and will work on most web hosts and server environments.
We are going to first need to create a source command which is going to match specific urls. Since we are targeting the WordPress day and name structure, we are creating the below regex:
^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(?!page/)(.+)$
This pattern will capture urls that contain a specific format of 4 digits, followed by 2 sets of 2 digits, and then a trailing collection of words. Our new target or destination url is going to be formatted like the following:
domain.com/blog/$4
Note: in this example we are adding /blog into our url structure.
If you are on another web host that does not offer a section to manage redirects you can modify your .htaccess file with a redirect rule using this same pattern. This would be formatted like the following:
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/(?!page/)(.+)$ https://domain.com/blog/$4
A nice and easy solution to quickly solve this change in bulk. Another excellent tool we recommend is the Permalink Helper from Yoast. This will allow you to quickly generate the correct redirect command depending on the permalink change you need.
We hope this helps! Need help with WordPress? Toss us an email at [email protected].