Content MarketingPartnersSearch Marketing

WordPress: Find and Replace All Permalinks In Your Database using Regular Expressions (Example: /YYYY/MM/DD)

With any site that spans over a decade, it’s not uncommon that there are many changes made to the permalink structure. In the early days of WordPress, it wasn’t uncommon for the permalink structure for a blog post to be set to a path that included the year, month, day, and the slug of the post:

/%year%/%monthnum%/%day%/%postname%/

Aside from having an unnecessarily long URL, there are a couple of other issues with this:

  • Potential visitors see a link to your article on another site or on a search engine and they don’t visit because they see the year, month, and day that your article was written. Even if it’s an amazing, evergreen article… they don’t click on it because of the permalink structure.
  • Search engines may deem the content as unimportant because it’s hierarchically several folders away from the home page.

When optimizing our clients’ sites, we recommend that they update their post permalink structure to:

/%postname%/

Of course, a major change like this can cause setbacks but we’ve seen that over time the advantages far outweigh the risks. Keep in mind that updating your permalink structure does NOTHING to redirect visitors to those old links, nor does it update internal links within your content.

How To Update Your Permalinks In Your WordPress Content

When you make this change, you may see some drop in your search engine ranking on those posts because redirecting the link may drop some authority from backlinks. One thing that can help is to properly redirect traffic that’s coming to those links AND to modify the links in your content.

  1. External Link Redirects – you must create a redirect on your site that searches for the regular expression pattern and properly redirects the user to the appropriate page. Even if you fix all internal links, you’ll want to do this for external links that your visitors are clicking on. I’ve written about how to add a regular expression (regex) redirect in WordPress and specifically about how to do the /YYYY/MM/DD/ redirect.
  2. Internal Links – after you update your permalink structure, you still may have internal links in your existing content that are pointing to the old links. If you don’t have redirects set up, they will result in you getting a 404 not found error. If you do have redirects set up, it’s still not as good as actually updating your links. Internal links have been proven to benefit your organic search results so reducing the number of redirects is a great step in keeping your content clean and accurate.

At issue here is that you need to query your posts data table, identify any pattern that looks like /YYYY/MM/DD, and then replace that instance. This is where regular expressions come in perfectly… but you still need a solution to iterate through your post content and then update the instances of the links – without messing up your content.

Thankfully, there’s a great solution out there for this, WP Migrate Pro. With WP Migrate Pro:

  1. Select the table you wish to update, in this case, wp_posts. By selecting a single table, you minimize the resources that the process will take.
  2. Insert your regular expression. This took a bit of work for me to get the syntax correct, but I found a great regex professional on Fiverr and they had the regex done in a matter of minutes. In the Find field, insert the following (customized for your domain, of course):
/martech\.zone\/\d{4}\/\d{2}\/\d{2}\/(.*)/
  1. The (.*) is a variable that’s going to capture the slug from the source string, so you have to add that variable to the Replace string:
martech.zone/$1
  1. You must click on the .* button to the right of the replace field to let the application know that this is a regular expression find and replace.
WP MIgrate Pro - Regex Replacement of YYYY/MM/DD permalinks in wp_posts
  1. One of the nicest features of this plugin is that you can actually preview the changes before executing them. In this case, I could immediately see what edits were going to be made to the database.
WP Migrate Pro - Preview of Regex Replacement of permalinks in wp_posts

Using the plugin, I was able to update 746 internal links in my content within a minute or so. That’s a heck of a lot easier than looking each link up and trying to replace it! This is just one small feature in this powerful migration and backup plugin. It’s one of my favorites and it’s listed on my list of best WordPress plugins for business.

Download WP Migrate Pro

Disclosure: Martech Zone is an affiliate of WP Migrate and is using it and other affiliate links in this article.

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.