WordPress network domain mapping fix

I’ve just been on an interesting little journey that started last September when I discovered that some of the sites on one of my WordPress networks had stopped working.

You might enjoy a little schadenfreude if I admit now that it was because I had a brilliant idea and did something stupid. I’ve posted details here in the hope that (a) if I do it again, I can find out how to fix it, and (b) if you’ve done it too, you’re closer to the solution than I have been for the past six months. I’ve ‘genericed’ the details to help you map it to your own setup.

The network setup

WordPress installed in top level folder (/), originally stand-alone but converted it to subfolder MU and kept it pretty much up to date. As of today, version 3.8.1. Some plugins, nothing fancy or particularly relevant except Donncha and co.’s WordPress MU Domain Mapping plugin, installed in wp-content/plugins and network activated in accordance with the current installation instructions.

The first blog was set up as the ‘network’, let’s call it firstblog.tld. Other blogs added and mapped thus: firstblog.tld/secondblog mapped to secondblog.tld; firstblog.tld/thirdblog mapped to thirdblog.tld; [and so on to] firstblog.tld/nthblog mapped to nthblog.tld.

I kept a couple of blogs which were not mapped to a domain under firstblog.tld, e.g. firstblog.tld/coolblog and firstblog.tld/hotblog.

The brilliant idea

I wanted to change the network so that it wasn’t firstblog.tld any more. I wanted it to be  thirdblog.tld. All I had to do was edit the entries in the wp_site, wp_blogs and wp_domain_mapping tables.

The stupid thing

I edited the entries in the wp_site, wp_blogs and wp_domain_mapping tables.

The result

All of the mapped domains worked fine. I thought I was a genius. However, the non-mapped blogs stopped working. All I got was a 404 – not found error, as if I’d tried to navigate to a non-existent blog, e.g. firstblog.tld/notablog.

The attempts to fix it

There’s no point in detailing everything I did to try to fix this. Essentially, I went back into the database and put the tables back the way I thought they were. Notice the important words in that sentence, “I thought”.

Six months of fiddling about with (and frankly, learning a lot about) .htaccess, wp-config.php, DNS, plugins, WordPress, Apache and internet WP forums got me nowhere.

Eventually, I had cause to set up a brand spanking new WP network and took the opportunity to study in detail the differences between the new (which worked properly) and the old (which didn’t). I found the cause, and thereby the solution.

The cause and the solution

The domain mapping plugin does not like it if you try to map the network to a domain. If the network is installed at mynetwork.tld there will be an entry for this domain in the wp_blogs table, probably as blog ID 1. If there is also an entry for this domain in wp_domain_mapping then the unmapped subfolder sites will return 404 – not found.

In hacking about with the tables (see “The stupid thing”, above), I had introduced this error. All I had to do was delete the firstblog.tld entry from the wp_domain_mapping table, and all the non-mapped subfolder blogs started working.

The outcome

Amongst other things, I got my CPD log back. Note the irony.