Skip to content

Allow multiple domains to resolve to the same network site

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

If there is a need to allow multiple domains to resolve to the same network site, 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 site on a multisite network that does not have a matching domain in the Site Address (URL).
  • Allowing a network site to be accessible from more than one URL.
  • Serving a specific part of a site (e.g., the home page, the site’s REST API) at a second domain.

Point a domain at a non-matching network site

Custom code can be added to client-sunrise.php to enable a domain to point to a network site 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 home page, the site’s REST API) at a second domain. This code example shows a method for serving the WP REST API for the main 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
}

Errors caused by a non-matching domain

It is possible for ms_site_not_found errors to be observed in an environment’s Runtime Logs. ms_site_not_found errors occur when the DNS of a domain is pointed to a WordPress multisite environment, and:

  • No network site on the environment matches the domain being requested.
  • No matching domain is configured in the environment’s sunrise.php.

To prevent the error from appearing in Runtime Logs, first identify the domain related to the logged ms_site_not_found error(s).

  • If possible, update the domain’s DNS to no longer point at VIP.
  • Remove the domain from the application’s domain mapping, preventing VIP from routing requests for that domain to the multisite application.
  • If supported by the DNS provider, update the DNS to redirect to a supported domain.
  • Route the domain to an existing network site on the environment using one of the methods above.
  • Review the settings of any third-party services or scripts that request the non-matching domain and correct outdated domain information.

The ms_site_not_found errors sometimes occur for a short time after a site launch. This is normal and expected and will typically resolve on its own. 

Last updated: December 26, 2023

Relevant to

  • WordPress