Updating the Hostname in the WordPress Database
When migrating a WordPress site from one server to another, it is crucial to update the hostname (domain) in the database. This ensures that all site functionalities operate correctly on the new environment.
Exporting and Importing the Database
First, export your MySQL database from the old server. Then, log in to the new server and create a blank database where you will import the previously exported data.
You can use phpMyAdmin or the MySQL command line, depending on the tools provided by your hosting provider.
Updating URLs in the Database
Once the data is imported, connect to the new database and update all references to the old domain. This step prevents issues with links, images, media, and dynamic functionalities.
The most important fields to update are located in the following tables: wp_options, wp_posts, and wp_postmeta.
Use the SQL queries below, replacing http://www.oldurl
with your old domain and http://www.newurl
with the new one:
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://www.oldurl', 'http://www.newurl')
WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = REPLACE(guid, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://www.oldurl', 'http://www.newurl');
Updating Table Prefixes (if applicable)
If you changed the table prefix during the migration (e.g., from wp_
to custom_
), make sure to also update the table names in the SQL queries above accordingly. Otherwise, the changes will not be applied.