Move a WordPress site
| Files and database | |
| wp-config.php | |
| The address of the site | |
| .htaccess | |
| Settings and links |
A WordPress site is two things: the files (WordPress itself, themes, plugins, uploads) and a MySQL database with the posts and settings. A move copies both and tells WordPress where the database and the site now are. What to arrange with the old provider first: Moving a website.
Files and database
- Create the site at the new hosting and copy all files of the old site into its folder.
- Export the database at the old hosting, in phpMyAdmin with Export, to an .sql file.
- Create a database and a user at the new hosting. Give it a long random password and note the database name, the user name and the host.
- Open phpMyAdmin of the new database and import the .sql file with Import.
wp-config.php
In the root folder of the site, set the new database credentials:
vi wp-config.php
/** The name of the database for WordPress */ define( 'DB_NAME', 'example_db' ); /** Database username */ define( 'DB_USER', 'example_user' ); /** Database password */ define( 'DB_PASSWORD', 'a-long-random-password' ); /** Database hostname */ define( 'DB_HOST', 'localhost' );
The address of the site
If the domain changes too, set the new address in the same file. These two lines win over the address stored in the database:
define( 'WP_HOME', 'https://www.example.com' ); define( 'WP_SITEURL', 'https://www.example.com' );
.htaccess
WordPress sends every address that is not a real file or folder to its index.php. On Apache this needs the standard block in .htaccess in the root folder; more about the file in .htaccess:
# 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
Settings and links
Log in at https://www.example.com/wp-admin, open Settings, General, check WordPress Address (URL) and Site Address (URL) and save. Posts still contain links and image paths with the old domain. With WP-CLI on the server, replace them in the whole database, first as a dry run:
wp search-replace 'https://old.example.com' 'https://www.example.com' --dry-run
Then run it again without --dry-run. Old addresses that visitors still use should answer 301 to the new ones, see Redirects.
Article authors: Andrei Olegovich, Klavdiya Kondakova