Moving a WordPress site: Changing URL and/or sub-folder from the command line

When maintaining a WordPress site, you might find it challenging to find the settings to update to properly convert your site from one URL to another, or from a root directory to a sub-directory.

For example, you might want to update the site of an event that happens every year, where previous years will be relocated to /year/ while the current event occupies the main domain...

This is a short reminder list of what should be updated. This covers sites with multi-site configured.

Changing domains

If you are "only" looking for a domain change, you need to update the following:

  1. On disk: locate wp-config.php at the root of your site, edit it, change the DOMAIN_CURRENT_SITE value for the new domain
  2. In the database: locate the wp_options table (name may vary depending on the prefix you selected at install time), then find the records where option_name is siteurl or home: "SELECT * FROM wp_options WHERE option_name IN ('siteurl','home');". Change those for the new domain (use the same form: keep the protocol and don't end with a slash)
  3. In the database: localte the wp_blogs table (see above regarding name), there is probably only one record there ("SELECT * FROM wp_blogs;") and update the domain (that's the bear domain without protocol nor slashes)
  4. In the database (only if using multisite), locate the wp_site table and update the domain (without protocol nor slashes)

That should be it for pure domain changes. Things get slightly more complex when changing the site to a sub-folder or to another domain *and* a sub-folder.

Changing to sub-folder

In the following example, we assume a domain of "source.example.com" moving to another domain "dest.example.com" you are moving from the root directory ("/") to year 2021 ("/2021/")

  1. On disk: locate wp-config.php at the root of your site, edit it, change the DOMAIN_CURRENT_SITE value for the new domain ('dest.example.com'). Also define the PATH_CURRENT_SITE setting to '/2021/'
  2. In the database: locate the wp_options table (name may vary depending on the prefix you selected at install time), then find the records where option_name is siteurl or home: "SELECT * FROM wp_options WHERE option_name IN ('siteurl','home');". Change those for the new domain (use the same form: keep the protocol and don't end with a slash), like so 'https://dest.example.com/2021'.
  3. In the database: localte the wp_blogs table (see above regarding name), there is probably only one record there ("SELECT * FROM wp_blogs;") and update the domain (that's the bear domain without protocol nor slashes). Like so: "UPDATE wp_blogs SET domain = 'dest.example.com', path = '/2021/' WHERE domain = 'source.example.com';"
  4. In the database (only if using multisite), locate the wp_site table and update the domain (without protocol nor slashes): "UPDATE wp_site SET domain = 'dest.example.com', path = '/2021/' WHERE domain = 'source.example.com';"
  5. Finally, and this is specific to sub-folders (not needed with simple domain changes), find the /.htaccess file at the root of your WordPress site and set RewriteBase from / to /2021/

With these changes, your site should work as soon as the possible cache has cleared.

Please note that your site might have been implemented in a (bad) way that used absolute URLs in HTML code or pseudo-code, and that you will need to convert these manually. If you want to skip that manual action, you could also dump the database into an SQL file and proceed with a string replacement with the sed command, then restore that dump on top of your database. Files (in your themes, etc) can also be replaced with a combination of sed and find.