≡ Menu

Replace URL paths with Search Regex plugin

Website changes are very common in bloggers’ world – Migrating from blogger platform to WordPress, changing permalink structure and moving content to a new domain. I did all of these which resulted in the below issues.

  1. URL links in posts that are with old permalink structure. They are like mydomain.com/2013/05/some-post.html.
  2. URL links pointing to old domain like myolddomain.com/2013/05/some-post.html or myolddomain.com/some-post/

I have 301 redirects from the old domain and the WordPress automatically takes care of redirection for permalink changes ,so I never bothered about updating these links much.

But recently I wanted to cleanup all this mess and looked for a plugin that can take care of this automatically. I found this great plugin Search Regex. Initially it did not seem to be working, but after playing with regex for some time, I was able to make it work and cleanup all old links. Below you will find steps on how to use this plugin.

Firstly, install the plugin and activate it. From WordPress admin dashboard go to Tools-> Search Regex.

How to use the plugin

  1. Take a backup of your database. In case if something goes wrong with the below process, you would be able to restore databases to the old state. There are many plugins available for this.
  2. In ‘Source’ drop down list select ‘Post Content’
  3. You need to provide regex search and replace patterns in the tool. The examples I have in this post would help you in coming up with the regex pattern for your case.
  4. Select regex check button
  5. Click on ‘Replace’. This provides a preview of the old URL being replaced and the new URL. Go through all the results and make sure that the regex is doing what you wanted. If it’s doing unnecessary replacement, work on changing the regex to achieve exactly what you wanted to do.
  6. If the results look good, click on ‘Replace & Save’ button. Note that this can’t be reversed in the tool. The only way to reset to old state is to use the backup created in step 1.

Replace links with old permalink structure

I had links like this technlg.net/2012/02/my-post.html. This were to be replaced with technlg.net/windows/my-post/

If you do not have category in the URL like mine, you can use below regex patterns.

In the ‘Search pattern’ field enter

"mynewdomain.com/[0-9]*/[0-9]*/([a-z0-9-]*)\.html"

In Replace pattern

mynewdomain.com/$1/

Replace links with old domain and old permalink structure

In this step I updated the links pointing to old domain with new domain and new URL structure. So the links like myolddomain.com/2012/04/my-post.html were to be replaced with mynewdomain.com/my-post/

In the ‘Search pattern’ field enter

"myolddomain.com/[0-9]*/[0-9]*/([a-z0-9-]*)\.html"

In Replace pattern

mynewdomain.com/$1/

Replace links with old domain but new permalink structure

In this step I updated the links that were pointing to old domain but with new permalink structure.

Enter below values for search patern

"myolddomain.com"

In Replace pattern

mynewdomain.com
{ 3 comments… add one }
  • Laurence March 26, 2018, 2:33 pm

    I know this post is a bit old, but I just wanted to thank you – Search Regex is poorly documented and I just wanted to make the change you describe to clean up my internal permalink structure – I followed your instructions and it worked perfectly for removing the dates and .html from my internal links. Thanks so much!

    • nlgtechblog April 1, 2018, 6:10 pm

      Glad that this post helped you restructure URLs on your site.

  • Darren C March 30, 2019, 6:12 am

    My most sincere, deepest, biggest thank you. That was just as you said, it worked perfect.

Leave a Comment