Recently we needed to change a multisite main domain for a large website. We concluded that we need to change code in 5 places as follow:
- wp_options table and wp_#_options tables: options named “siteurl” and “home” ;
- wp_site table ;
- wp_sitemeta: the option named “siteurl” ;
- wp_blogs: any entries in the “domains” column that have the old domain name ;
- wp-config.php file.
So here are the steps we had made in order to change primary domain for a wordpress multisite:
1. Backup your mysql database ;
2. Export the theme settings if you have that option ;
3. Create a temporary directory where you will dump the data ;
4. Save the following script in temporary directory ;
#!/bin/bash #get all tables contain name options for i in `mysql -u user -p'password' dbname -e "show tables;" | grep options`; do mysqldump -u user -p'password' dbname --skip-add-drop-table --no-create-info --replace $i --where="option_name='siteurl' or option_name='home'" > $i done mysqldump -u user -p'password' dbname --skip-add-drop-table --no-create-info --replace wp_site > wp_site mysqldump -u user -p'password' dbname --skip-add-drop-table --no-create-info --replace wp_blogs > wp_blogs mysqldump -u user -p'password' dbname --skip-add-drop-table --no-create-info --replace wp_sitemeta > wp_sitemeta #replace domain in dump tables for i in `ls | grep wp` do sed -i 's#old-domain.com#new-domain.com#g' $i done #reimport data for i in `ls | grep wp` do mysql -u user -p'password' dbname < $i done
5. Change the values of user, password, dbname, old-domain.com, new-domain.com with your values and execute it (if your tables names are custom change with correct ones.) ;
6. Change the values in your wp-config.php file ;
define('WP_SITEURL', 'http://new-domain.com'); define('WP_HOME', 'http://new-domain.com'); define('DOMAIN_CURRENT_SITE', 'new-domain.com');
7. Adjust apache/nginx config to reflect new domain name ;
8. Login as admin and import theme settings ;
9. Adjust the values in theme options if there any (usally the logo path, favicon path, etc.) ;
10. Empty cache if any.
Now everything should work with the new domain name.