For various reasons, we sometimes need to change the domain name of WordPress, and the domain name of WordPress is written directly into the database. It is obviously not possible to directly change the domain name bound to the WordPress site in the server. In addition to this work, we also need to replaceWordPress databaseOnly when the old domain name in is the new domain name can the WordPress domain name replacement be completely completed. In this article, I will introduce you to several ways to change your WordPress domain name. I need to remind everyone in advance that before modifying the WordPress database, you must make a backup just in case.
If what you need is not to change domain names, but to bind multiple domain names to a WordPress site, please refer to another article on this site:How to bind multiple domain names in WordPress。
Use the wp-cli tool to search and replace the domain name to replace the WordPress domain name
wp-cli is a command line tool that allows us to install and update WordPress through the command line, and perform some batch operations on WordPress. It is very convenient to use.
Install wp-cli tool
If already installedwp-clitool and proceed directly to the next 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 domain names in WordPress database
The wp-cli tool provides us with a command to search and replace characters in the database. Execute the following command directly in the root directory of WordPress to complete the operation of changing the WordPress domain name. This command supports some options to facilitate our customized operations. See detailsOfficial description of wp search-replace command。
wp search-replace old.com new.com
Use SQL statements to replace domain names directly in the database
Just run the following command directly in phpMyAdmin to replace the old domain name with the new domain name. After copying the code, replace old.com, new.com, if your WordPress site uses a custom data table prefix, and modify the ‘wp_’ data table prefix in the following command.
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, the domain names written into WordPress exist in the above data tables, but it is not excluded that custom data tables added by themes or plug-ins also contain old domain names. If after executing the above command, there are still some old domain names that have not been replaced, you can refer to the method below to directly edit the .sql file to replace them.
Use the code editor to edit the .sql export file and replace it directly
Export the .sql file, then open the exported .sql file with your favorite code editor, search and replace the old domain name with the new domain name.
Then directly delete all data tables in the site database and then import them into the database.
Set up a 301 redirect to direct traffic from the old domain name to the new domain name
If your site has been indexed by search engines, you can set up a 301 redirect in Nginx to import traffic from the old domain name to the new domain name.
server {
listen 80;
server_name old.com new.com;
return 301 http://new.com$request_uri;
}
Finally, I would like to remind you again that you must make a backup before modifying the WordPress database. If something goes wrong during the modification process, we can easily restore the data without causing too much loss.
