Several Ways to Change a WordPress Site to a New Domain During Migration

For a variety of reasons, we sometimes need to change the domain name of a WordPress site. But the WordPress domain is written directly into the database, so simply changing the domain bound to the server is not enough. To complete the migration properly, we also have to replace the old domain with the new one inside the WordPress database. In this article, I will introduce several ways to do that.

One thing needs to be said clearly before anything else: before modifying the WordPress database, always make a backup, just in case something goes wrong.

If what you need is not a full domain replacement but binding multiple domains to one WordPress site, that is a different problem and should be handled separately.

Use wp-cli search-replace to change the WordPress domain

wp-cli is a command-line tool that lets us install and update WordPress, and it also gives us a convenient way to perform batch operations. It is very handy for tasks like domain replacement.

Install wp-cli

If wp-cli is already installed on your server, you can skip this step.

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Search and replace the old domain in the WordPress database

wp-cli includes a command specifically for search-and-replace operations in the database. Run the following command from the WordPress root directory to replace the old domain with the new one. The command supports additional options, so for more control you can refer to the official wp search-replace documentation.

wp search-replace old.com new.com

Replace the domain directly with SQL statements

You can also run SQL directly in phpMyAdmin. After copying the statements below, replace old.com and new.com with your actual domains. If your WordPress site uses a custom table prefix, also change the wp_ prefix in the statements.

UPDATE wp_options SET option_value = replace(option_value, 'http://old.com', 'http://new.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://old.com','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://old.com', 'http://new.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://old.com','http://new.com');
UPDATE wp_usermeta SET meta_value = replace(meta_value, 'http://old.com', 'http://new.com');
UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'http://old.com', 'http://new.com');
UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'http://old.com','http://new.com');

In most cases, those tables are where the old domain is stored. Still, it is possible that a theme or plugin writes the old domain into custom tables too. If some old URLs remain after running the SQL above, the next method can help.

Edit the exported .sql file directly in a code editor

Export the database as a .sql file, then open that file in your preferred code editor and search and replace the old domain with the new one.

After that, drop the existing tables from the site database and import the edited SQL file back into the database.

Set up a 301 redirect to send traffic from the old domain to the new one

If the old domain has already been indexed by search engines, it is worth setting up a 301 redirect in Nginx so traffic and SEO value are forwarded to the new domain.

server {
    listen 80;
    server_name old.com new.com;
    return 301 http://new.com$request_uri;
}

And one last reminder: always make a backup before modifying the WordPress database. If anything goes wrong during the replacement process, a good backup makes recovery much easier.

Related Posts

Leave a Reply

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