The easy way to migrate WordPress using search and replace database

So you want to migrate a WordPress database and you came into the problem that when you load the new installation directory you are being redirected to the old domain. You probably tried to manually replace in your PhpMyAdmin in the wp_options  table the two entries that normally exist, but still no success. Maybe your website is loading but there are lots of errors in there.

This is because WordPress and lots of plugins store data and full URI in the database using wp_serialize function. Here is a little example about what I am talking about:

If you just do a search and replace you will break all this fields and data can not be recovered.

WordPress itself in his Codex Archive about moving WordPress installation files and database recommends that you manually change the URL in the WordPress Control backend.

change url in wordpress backend

Doing this, will give you a 404 error page on the old domain, but after you move the files and database on the new server, your WordPress installation should work.

Please note that this process leaves you with lots of database entries containing the old domain URL. Entries like posts, data stored by themes and plugins will not be modified. On the other hand you can do a search and replace on your entire database to change the URLs, but this can cause major issues with serialized data because lots of themes plugins and widgets store values along with their length. This can be a big headache.

I am sure there are lots of tools out there, but today I will be talking about this PHP Search/Replace tool. Note that this tool is not specially designed for WordPress migration, but for any database search and replace process. It works great with WordPress installation and like you can see it has a very friendly interface. After you finish searching and replacing the database you can use the build in cleanup process or delete the files manually.

wodpress search and replace database

Please note that I am not related in any way with this guys. I am just happy to use their tool and post about it.

To run the search/replace script you will have to download it and place a copy on your website root folder. So your files will be something like this:

It is very important to place the script in his own folder because when it finishes the process it will attempt (give you the option of course) to delete himself. This will delete all files on the same level so if you place all the files directly on the root you will end up deleting all WP installation files. If you do not understand this just ask a geek for a little advice.

Remember that if you had any kind of permalinks rules it is very possible that they will not work properly after the WP migration. So you must disable .htaccess  and reconfigure permalinks.

Resetting the permalinks is quite easy and can be done in two ways:

  • The first method is done in the WordPress dashboard. Just go to your WordPress dashboard, click on Settings and go to Permalinks.

reset wordpress permalinks

Just click Save Settings and you should be good to go. If this does not work try to change to a different option, save and then change back to your desired permalink structure. This will force WordPress to rewrite the .htaccess  file.

  • The second option is to manually edit the database. You will have to search your database wp_options table for a special entry called  permalink_structure . After you found it just edit it there.

manualy edit permalink for wordpress

Note that if you edit your settings via database or even if you are using the dashboard but WordPress does not have permission to write on file. You will have to manually edit your  .htaccess

If all this did not make your site to work properly, try to do a force refresh, clear cache or just use a different browser 😉

One thought on “The easy way to migrate WordPress using search and replace database”

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × 2 =