Redirect the main site of a WordPress multisite network

Before I start with my first “Project 52” blog post, I want to apologize to all my English readers for not being version active. If you don’t know German, you didn’t have too much to read on my blog in the past. It was acutaly only one real post in the last 4 years (and three posts with slides from my WordCamp talks) only. But this is going to change now!

For those of you who can read German, you might have read my advent calendar in 2015, followed by #projekt52 (a blog post every week of the year) and another advent calendar in 2016. As I was very unhappy with the fact, that I haven’t blogged in English too much, I’ve made a decision: I will blog every two weeks in English and on every other week, I’ll translate this blog post into German, starting today!

A multisite with no main site

This week, I finished the migration of a large client project into a new multisite installation. This multisite had the goal to have one sub-site per customer of our client. Every multisite needs a main top level domain. For this example, let’s say the name of the domain was abc-customers.com. The multisite was set up to use subdomains, while working on the draft of a new customer sub-site. So, we would have something like somecustomer.abc-customers.com and anothercustomer.abc-customers.com. Once the sites where ready to go live, the sub-site were switched to a domain or subdomain (e.g. abc.somecustomer.com and anothercustomer-platform.com). I was using the core WordPress functionality to map those domains and no additional domain mapping plugin.

This all works fine. Any URL on those sites would only have the mapped domain names and the subdomain used by the sub-site before were no longer visible. Still there were some issue with that. First of all, the domain abc-customers.com was still a regular sub-site of the multisite network. And secondly, a call to any random subdomain would redirect to the “site registration page” (the public registration was disabled) and the main domain was visible again.

Using a blank/maintenance page

I first thought about using a blank page for domain of the main site. But I couldn’t find a nice “blank theme” for that page and I actually didn’t like the idea of showing the visitor “nothing” and asking him to click on some link to go somewhere else. So, I thought, a redirect would be a lot better.

Redirecting the whole domain

The first idea one of my colleagues had, was redirecting the whole abc-customers.com domain to another domain. But this is not going to work, as the domain is mandatory to be able to access the network administration for the multisite installation.

Frontend redirects only

So, we needed to limit the redirect only to the frontend pages of the main site. In order to solve this, I came up with a very small plugin, that will do those redirects:

function multisite_mainsite_redirect() {
	if ( is_main_site() && ! is_admin() ) {
		wp_redirect( 'https://example.com/' );
		exit();
	}
}

add_action( 'init', 'multisite_mainsite_redirect' );

Looks easy, eh? As it turned out, it was a bit too easy. There are some edge cases that needs to be taken into account.

The registration page

Even if you activate the plugin for the whole multisite network, it will not catch all pages. One page it was not working on, was the registration page for new multisite sub-sites. Even if you deactivate the public registration, the page will still render showing a message with a notice. So, I had to add a condition to redirect that page as well (I’ll show you the code in the result in a bit).

Conflicts with WP-CLI

After changing the conditions in the plugin, I thought I would have solved all cases. But while migration the sites from the staging server, I used the WP-CLI to update all URLs to the mapped domains. On any WP-CLI command, I got a warning and the command was not executed:

$ wp
Warning: Some code is trying to do a URL redirect. Backtrace:
#0 WP_CLIUtilswp_redirect_handler(http://example.com/)
...

To get the plugin work with the WP-CLI, I had to use another additional condition.

The result

After the those changes, I ended up with this little function:

function multisite_mainsite_redirect() {
	if ( is_main_site() && ! is_admin() && 'wp-login.php' != $GLOBALS['pagenow'] && ! defined( 'WP_CLI' ) ) {
		wp_redirect( 'http://example.com/' );
		exit();
	}
}
add_action( 'init', 'multisite_mainsite_redirect' );

The function is using a static redirect. But if you would like to redirect to another sub-site or a dynamic URL, that could also easily be done in the function.

Conclusion

I’ll hope you find that little plugin useful. As usually, I’ve published the plugin code as a GIST where you can simply download it as a ZIP file and install it to your site. If you have any additional questions, please leave a comment below.

And I also hope you liked the first blog post of my little “bilingual fortnightly blog posting challenge” 😉 If you have any topics, you would like me to blog about, please share your ideas.

Hopefully this first blog post after quite a long time didn’t had too many typos or grammatical errors 😉

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

12 comments » Write a comment

    • Because it’s not possible with the htaccess file, as it would completely block access to the network administration and it you can not check the current page with the conditional functions.

  1. Hi Bernhard, I’ve being looking for this solution for a few hours and gone mad with .htaccess useless tricks when I came across this post. Really, it works out of the zipped box. You made my day. Thank you so much for sharing!

    Now I have a question although I don’t really need that extra stuff — just a theoretical question.

    I assume that you did not activate the plugin on the network but only on the main site, i.e. abc-customers.com, right? That would make sense since you don’t want sub-sites home pages to be redirected elsewhere. If so, I assume that the plugin is listed in the plugin dashboard of in somecustomer.abc-customers.com and anothercustomer.abc-customers.com but not activated. Is it a problem? Aren’t you afraid that an admin of those sub-sites wonder what it is or activate it by mistake?

    • Hi Christian,

      it is activated in the network (which is necessary). But the condition `is_main_site()` makes sure it’s only redirecting the main page.

  2. Wow. Thank you so much Bernhard. This was driving me nuts. I had been going down the same process you mentioned. This works perfectly.

  3. This might be a stupid question but I tried to use the plugin and can’t find how to add the url I want it to redirect to… am I missing something?

    • Hi Farah, the plugin does not have any options in the dashboard, just download the file and replace the string `http://example.com` with the URL you want to rewrite to. Then upload it to your website and activate it.

      • Ah I see, I think the problem is that I either don’t know where to change the string which I think I need to find using Editor which has disappeared from my dashboard settings…

        • You should never use the “Editor” in the dashboard. Use a FTP program to modify the file. Or how have you installed the plugin in the first place?

  4. Second this – lovely neat solution to the same issue we’ve had: main site no longer required but want to maintain all the child sites.

    As an aside, I didn’t network enable this, just enabled at the top site only and it works a treat.

Leave a Reply

Your email address will not be published. Required fields are marked *