WordPress multisite with multiple domains

July 16, 2014

One of the reasons why I decided to migrate to WordPress rather than another blogging product, is because WordPress is a platform you can use to build more complicated sites on. One feature (that is now built in) is multisite – it allows you to setup multiple sites sharing a single Wordpress installation. The idea originally was to create a “network” of related sites, but you can use it to create sites that are completely separate from each other – each site can have it’s own plugins, themes and even users. Setting it up wasn’t quite as simple as I expected, so here is what I did to get it to work.

The first part of setting up multisite is to edit your wp-config.php adding a flag to enable WordPress multisite:

define('WP_ALLOW_MULTISITE', true);

Add that before the “That’s all, stop editing! Happy blogging.” line. Once that’s done in the admin panel you will find a new option Settings -> Network setup. This sets up your database (remember to take a backup first, I use Updraft Plus to put weekly backups on Amazon S3) and alters the config to enable the network.

www vs non-www

This gave me a warning though because my domains were prefixed with www. I usually setup sites to be hosted on the www subdomain and redirect the root domain to that through Nginx (or whatever web sever I’m using). This ensures search engines don’t see the different subdomains as different sites – having duplicated content on different sites impacts SEO. The warning said that removing the www prefix would still mean the content would be available and not break anything, so I proceeded to remove it but forgot about the redirect on Nginx resulting in an inaccessible site. I removed the redirect, but then everything was only accessible under stackednotion.com, going to www.stackednotion.com redirected there, but I wanted it the other way around.

I restored the latest backup and took a look again. The warning said the site url” needed to be changed, this is what is in the WordPress config:

Screen Shot 2014-07-14 at 14.06.00

So which one is the “site url”? I thought the second one, so changed that and tried again… nope. By “site url” it really means WordPress Address (URL), so I changed that to http://stackednotion.com.

When setting up the network it will give you some settings to put in your wp-config.php and .htaccess files. As I’m running Nginx I ignored the later (it worked out of the box after removing the redirect), and the wp-config.php file had been changed automatically, so there was nothing to do there. After that Stacked Notion was available under www.stackednotion.com, with stackednotion.com redirecting there. Weirdly permalinks still referred to stackednotion.com, redirecting to www when going to them – I’m not sure how to change that, so if anyone knows let me know!

Multidomain

By default multisite is designed to be setup for multiple subdomains under a single root domain. As I already have www.stackednotion.com I could add jobs.stackednotion.com. I didn’t want that though, I wanted a new site under a separate domain _remotetechjobs.eu_.

The first part is to add a new site in WordPress, I created jobs.stackednotion.com (My Sites -> Network Admin -> Sites then Sites -> Add New) and verified this worked as expected (at first I got a blank screen, by default it uses the twentyfourteen theme which I had uninstalled, so check the theme this site is using if you get the same). Next I needed to set it up to run from a separate domain.

The way to go about this is through the WordPress MU Domain Mapping plugin. Once I installed that plugin (you need to download and install it manually rather than through WordPress) and activated it for the network (My Sites -> Network Admin -> Plugins), you setup everything through Settings -> Domain Mapping. Again this isn’t as simple as it should be. I left the IP address field blank to ensure that the installation remains portable, and entered stackednotion.com into the Server CNAME domain field. I left the options as the default – although if you want separate users you should disable Remote Login.

If you haven’t already, setup the DNS for your new domain to point at the server WordPress is hosted on and let it propagate. To then add the domain you go to Settings -> Domains. You don’t need to setup the original domain here (stackednotion.com in my case), so I just added remotetechjobs.eu with Site ID 2. You can get this from the wp_blogs table, but confusingly it is the blog_id value not site_id. After that going to remotetechjobs.eu mapped to the new WordPress site / blog.

Tidy up

To assist testing I setup Nginx to redirect any subdomains on stackednotion.com and remotetechjobs.eu to WordPress. Going to these where the site doesn’t exist gave an error saying registration was disabled. To avoid that I just setup a redirect in Nginx so that anything other than www redirected to the root domain.

That seemed to work, until I tried logging into remotetechjobs.eu. This failed as WordPress still thought it was hosted under http://jobs.stackednotion.com/, you can’t change the domain through the interface once multisite is enabled, so I did it in the database. The options for this site were stored in wp_2_options, so I replaced http://jobs.stackednotion.com/ with http://remotetechjobs.eu/ in there:

mysql> select * from wp_2_options where option_value like '%jobs%';
+-----------+-------------+-------------------------------+----------+
| option_id | option_name | option_value                  | autoload |
+-----------+-------------+-------------------------------+----------+
|         1 | siteurl     | http://jobs.stackednotion.com | yes      |
|         2 | blogname    | Remote Tech Jobs              | yes      |
|        33 | home        | http://jobs.stackednotion.com | yes      |
+-----------+-------------+-------------------------------+----------+
3 rows in set (0.00 sec)

mysql> update wp_2_options set option_value = 'http://remotetechjobs.eu' where option_id in (1, 33);

After that everything worked as expected!