Due to 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 You also need to replace the old domain name in the WordPress database with the new domain name to completely complete the WordPress domain name replacement.
#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.
Use the wp-cli tool to search and replace the domain name to change the WordPress domain name
wp-cli is a command line tool that allows us to install and update WordPress through the command line , which is quite convenient to use to perform some batch operations on WordPress.
Install the wp-cli tool
If you have already installed the wp-cli tool, 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 the 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 replacement. To operate the WordPress domain name, this command supports some options to facilitate our customized operations. For details, please see the official instructions of the wp search-replace command.
wp search-replace 'old.com' 'new.com'
Use SQL statements to directly replace the domain name in the database
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 does not rule out that custom data tables added by themes or plug-ins also contain old domain names. If the above is executed After the 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 a code editor to edit the .sql exported file and directly replace
Export the .sql file, then use your favorite code editor to open the exported .sql file and search 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 301 jump to import the traffic of the old domain name into the new domain name
If your site has been included by the search engine, you can make a 301 jump in Nginx Set up and import the traffic of 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.
For more WordPress technical articles, please visit the WordPress Tutorial column!
The above is the detailed content of How to change domain name in wordpress. For more information, please follow other related articles on the PHP Chinese website!