2.14.1.8. Change site address in WordPress

Attention!

Before performing any actions, be sure to create a backup of the site's files and database to be able to restore the site to its original state if something goes wrong.

Important points:

  • If the old or new address contains national alphabet characters (e.g. Cyrillic characters), it must be converted to punycode using Punycode converter.
  • The HTTP/HTTPS redirect set in the site settings must match the protocol in the new address. For example, if a certificate is connected to the site and redirect to HTTPS is set in the site settings, the new address should start with https://. In case of protocol mismatch, a cyclic redirect between the protocols http:// and https:// may occur on the site.
  • The redirect to an address with or without www must match the new address. For example, if a redirect to an address with www is set in the site settings, the new address should also have www. In case of protocol mismatch, a cyclic redirect between the address with www and without www may occur on the site.

This article describes different ways to change a site's address within WordPress itself. To rename a site in the "My sites" section, see Change site name.

  1. Open the "CMS install" section.
  2. Click "Change address".
  3. Enter the new site address and click "Change site address".

The system takes the name of the database used from the site configuration file and changes the old address to the new one.

Attention!

If table names in the database start with a different prefix instead of the standard wp_, change the prefix in SQL queries.
  1. In the site configuration file, check the name of the database to which it is connected.
  2. Open database in phpMyAdmin.
  3. Execute the following three SQL queries:
    UPDATE wp_options SET option_value = REPLACE(option_value, 'http://old-address.com', 'http://new-address.com') WHERE option_name = 'home' OR option_name = 'siteurl';
    UPDATE wp_posts SET guid = REPLACE(guid, 'http://old-address.com','http://new-address.com');
    UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://old-address.com', 'http://new-address.com');
  4. To change the site address in the metadata, additionally execute query:
    UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://old-address.com','http://new-address.com');
  5. If the site uses comments, execute two additional queries:
    UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'http://old-address.com', 'http://new-address.com');
    UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'http://old-address.com', 'http://new-address.com');
  6. Check the site's operation at the new address.
  1. Go to "Settings → General".
  2. In the "WordPress address (URL)" and "Site address (URL)" fields, specify the new address (must begin with http:// or https:// and no slash / at the end, e.g. http://example.com) and save the changes.
  3. Check the site's operation at the new address.

Attention!

Use with caution if the site database is used by multiple sites at the same time.

To change the address in WordPress, you can use a specialized utility WP-CLI (available on hosting by default).

  1. Connect to the hosting via SSH.
  2. Navigate to site root directory (use your own data instead of example.com/www):
    cd ~/example.com/www
  3. Change the site address in its database (instead of old-address.com specify the old site address, instead of new-address.com specify the new one):
    wp search-replace 'old-address.com' 'new-address.com'

    As a result, information about where and how many replacements have been made will be displayed.

  4. Check the site's operation at the new address.

You can customize the search with replacement more finely (e.g., only on certain tables or columns) by using additional attributes described in the WP-CLI documentation.

Content

    (10)