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:
1 |
a:6:s:4:"role";s:5:"admin";s:17:"includes_globally";s:2:"on";s:18:"pages_for_includes";s:0:"";s:12:"js_to_footer";s:2:"on";s:15:"show_dev_export";s:3:"off";s:11:"enable_logs";s:3:"off";} |
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.
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.
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:
1 2 3 4 |
/your-custom-folder-for-search-replace-script /wp-admin /wp-content /wp-includes |
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.
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.
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
1 2 3 4 5 6 7 8 9 10 11 |
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress |
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”