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 😉

Veröffentlicht von

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.

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.