WordPress: Remove and Redirect A YYYY/MM/DD Permalink Structure with Regex and Rank Math SEO
Simplifying your URL structure is a great way to optimize your site for a number of reasons. Long URLs are difficult to share with others, can get cut off in text editors and email editors, and complex URL folder structures can send the wrong signals to search engines on the importance of your content.
YYYY/MM/DD Permalink Structure
If your site had two URLs, which one would you think has provided the article with higher importance?
- https://martech.zone/permalink-optimization OR
- https://martech.zone/permalink-optimization
One of the default setups for WordPress is to have a permalink structure on the blog that includes the yyyy/mm/dd within the URL. This isn’t ideal for a couple of reasons:
- Search Engine Optimization (SEO) – As discussed above, the hierarchy of the site is basically showing search engines that the content is 4 folders away from the home page… so it’s not important content.
- Search Engine Result Page (SERP) – You may have a fantastic article on your site that you wrote last year but that’s still valid. However, other sites are publishing more recent articles. If you viewed a date structure that was a year ago in the search engine result page (SERP), would you click the older article? Probably not.
The first step to take is to update Settings > Permalinks in WordPress admin and just make your permalink the /%postname%/
This; however, would break all of your existing post links on your blog. After having your blog live for a while, it’s not fun adding redirects for every one of your old articles. That’s okay because you can utilize a Regular Expression (Regex) to do this. A regular expression looks for a pattern. In this case, our regular expression is:
/\d{4}/\d{2}/\d{2}/(.*)
The expression above breaks down as follows:
- /\d{4} looks for a slash and 4 numeric digits representing the year
- /\d{2} looks for a slash and 4 numeric digits representing the month
- /\d{2} looks for a slash and 4 numeric digits representing the day
- /(.*) captures whatever is at the end of the URL into a variable that you can redirect to. In this case:
https://martech.zone/$1
This is how it looks within the Rank Math SEO plugin (listed as one of our favorite WordPress plugins), just don’t forget to ensure the type is set to Regex with the dropdown:
Removing Blog, Category, or Category Names or Other Terms
Removing Blog – If you had the term “blog” within your permalink structure, you can utilize Rank Math SEO’s redirections to populate
/blog/([a-zA-Z0-9_.-]+)$
Notice on this, I didn’t use the (.*) option since that would create a loop if I had a page that was just /blog. This requires that there’s some kind of slug after the /blog/. You’ll want to redirect this just as above.
https://martech.zone/$1
Removing Category – To remove category from your slug (which is there by default) deploy the Rank Math SEO plugin which has an option to strip category from the URL structure in their SEO settings > Links:
Removing Categories – If you had categories, you’ll want to be a bit more careful and create an array of the exact category names so you don’t accidentally create a circular loop. Here’s that example:
/(folder1|folder2|folder3)/([a-zA-Z0-9_.-]+)$
Again, I didn’t use the (.*) option since that would create a loop if I had a page that was just /blog. You’ll want to redirect this just as above.
https://martech.zone/$1
Disclosure: Martech Zone is a customer and affiliate of Rank Math.