Skip to content

Multisites

Allow multiple domains to resolve to a single site

By default, the VIP Platform enforces the use of a single domain per site in a WordPress multisite.

If there is a need to allow multiple domains to resolve to a single site in the network, either temporarily or permanently, custom logic can be added to client-sunrise.php to handle this type of redirect without database lookups.

Example use cases:

  • Pointing a domain to a subsite that does not have a matching domain in the Site Address (URL).
  • Allowing a subsite to be accessible from more than one URL.
  • Serving a specific part of a site (e.g., the homepage, the site’s REST API) at a second domain.

Point a domain at a non-matching subsite

Custom code can be added to  client-sunrise.php to enable a domain to point to a subsite that does not have a matching domain in the Site Address (URL).

In this code example, the domain new.example.com is configured to map to Site ID 5. The added custom code enables requests to new.example.com to serve Site ID 5, even though it has the Site Address (URL) https://mytestmultisite.go-vip.net/site/.

client-sunrise.php
<?php

$extra_domains = [
	// 'domain' => blog_id
	'new.example.com' => 5
];

if (
	isset( $_SERVER['HTTP_HOST'] )
	&& array_key_exists( $_SERVER['HTTP_HOST'], $extra_domains )
) {
	$mask_domain = $_SERVER['HTTP_HOST'];

	// Set globals
	$blog_id      = $extra_domains[ $mask_domain ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	$current_blog = get_site( $blog_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	
	// This should always be 1, unless you are running multiple WordPress networks.
	$current_site = get_network( 1 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	$origin_domain = $current_blog->domain . untrailingslashit( $current_blog->path );

	add_filter( 'home_url', function( $url ) use ( $mask_domain, $origin_domain ) {
		return str_replace( $origin_domain, $mask_domain, $url );
	} );

}

Specific part of a site served from multiple domains

Custom code can be added to  client-sunrise.php to serve a specific part of a site (e.g., the homepage, the site’s REST API) at a second domain. This code example shows a method for serving the WP REST API for Site ID 1 from multiple domains: example.com, example.blog, or example.go-vip.net.

client-sunrise.php
// Allow REST API requests to be served on one of several domains.
$clientslug_custom_sunrise_domains = array( 'example.com', 'example.blog', 'example.go-vip.net' );

// Cause each of these domains to load `site_id` 1.
$clientslug_custom_sunrise_site_id = 1;

if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] )
	 && in_array( $_SERVER['HTTP_HOST'], $clientslug_custom_sunrise_domains, true )
	 && 0 === strpos( $_SERVER['REQUEST_URI'], '/wp-json' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, this is safe here.
) {
	// These domains are each associated with `site_id` 1 of network 1.
	$current_blog = get_site( $clientslug_custom_sunrise_site_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	// This should always be 1, unless you are running multiple WordPress networks.
	$current_site = get_network( 1 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}

Last updated: May 02, 2022