One-Off Script: Fix Yoast SEO Redirects When Switching Permalink Structure

If you use Yoast SEO to manage redirects and you change your permalink structure you may need to update a large amount of your redirects.

Here is a small simple script to help with that. This was made for when you switch from https://www.sethryder.com/2018/10/31/sample-post/to https://www.sethryder.com/sample-post/. If you are switching to something else it should be easy to modify, you will just need to update the regex in the preg_replace.

The two rows you will want to run through this script in your database are the wpseo-premium-redirects-export-plainand the wpseo-premium-redirects-basein wp_options. I assume they may vary if you are not using the premium version of the plugin.

This script will take the serialized options in the database (that you copy into the expected text files) and go through them and remove/update the redirect links. Once finished it writes them to the new files and you can replace the rows in your database.

WordPress: Switching Permalink Structure

Recently I had to migrate a blog from its own subdomain to the primary domain in a directory (Example: blog.domain.com to www.domain.com/blog).

Migrating a blog to a different domain is easy enough and I have done it countless times. The twist this time is that they also wanted to switch the permalink structure from https://www.domain.com/2018/10/30/sample-post/to https://www.domain.com/sample-post/.

After some quick research it appears there are few plugins for WordPress that will help you with this. But even some of them require manually setting up redirects for each blog post. Also I like to avoid unnecessary plugins as this adds more to maintain, especially when you are managing a large amount of sites.

So the easiest way to do this is just with a simple rewrite/redirect. Here is what I am using in nginx:

Make sure you update to the new permalink structure right before placing the rewrite rule as this will cause 404 errors if you haven’t switched to the new structure.

This should be easy enough to do with Apache and moving from other date based permalink structure. 

Give me a shout if you have a question and I will see if I can answer it.