Skip to content

Backgrounds

How-to Guides

Technical References

Redirects /

Domain redirects in vip-config.php

To use more than one domain per site, set up vip-config.php to handle redirecting secondary domains to the desired primary domain for each subsite. This is particularly useful for mapping domains on a multisite, where redirects between non-www domains and www variants do not occur automatically. 

The main advantage of this method is to respond to a request before WordPress is loaded, thus improving the speed and performance of the response. The caveat is that because WordPress has not loaded, you cannot rely on any WordPress APIs or functions.

When redirecting in the vip-config.php file, omit the /cache-healthcheck? URI from your redirects. This URI is used by the VIP Go platform to check the health of your site, and a redirect response will cause issues with your site being served correctly.

Here’s an example snippet of code that redirects requests for example.net or example.org to the same path on the main site, example.com; e.g. example.net/some/path would be redirected to example.com/some/path:

if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
	$http_host   = $_SERVER['HTTP_HOST']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	$request_uri = $_SERVER['REQUEST_URI']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	$redirect_to_domain = 'www.example.com';
	$redirect_domains   = array(
		'example.net',
		'www.example.net',
		'example.org',
		'www.example.org',
	);

	/**
	 * Safety checks for redirection:
	 * 1. Don't redirect for '/cache-healthcheck?' or monitoring will break
	 * 2. Don't redirect in WP CLI context
	 */
	if (
			'/cache-healthcheck?' !== $request_uri && // Do not redirect VIP's monitoring.
			! ( defined( 'WP_CLI' ) && WP_CLI ) && // Do not redirect WP-CLI commands.
			$redirect_to_domain !== $http_host && in_array( $http_host, $redirect_domains, true )
		) {
		header( 'Location: https://' . $redirect_to_domain . $request_uri, true, 301 );
		exit;
	}
}

In a multisite network, code such as the above is necessary to handle redirecting http://www.example.com to example.com (or vice versa) for each subsite. (In single-site installations, this redirect is handled automatically, and no additional configuration is necessary.)

Here’s another example for multisites where there may be redirects for multiple sites, from different domains, resulting in using non-www as the canonical version. Note that for A => [B, C], B and C redirect to A.

if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
	$http_host   = $_SERVER['HTTP_HOST']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	$request_uri = $_SERVER['REQUEST_URI']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	$redirect_domains = array(
		/*
		'to-this-domain.com' => array(
			'from-this-domain.com',
			'or-from-this-domain.com',
		),
		*/
		'example-one.com'   => array(
			'www.example-one.com',
			'www.exampleone.com',
		),
		'example-two.com'   => array(
			'www.example-two.com',
			'www.exampletwo.com',
		),
		'example-three.com' => array(
			'www.example-three.com',
			'www.examplethree.com',
		),
	);

	/**
	 * Safety checks for redirection:
	 * 1. Don't redirect for '/cache-healthcheck?' or monitoring will break
	 * 2. Don't redirect in WP CLI context
	 */
	foreach ( $redirect_domains as $redirect_to => $redirect_from_domains ) {
		if (
				'/cache-healthcheck?' !== $request_uri && // Do not redirect VIP's monitoring.
				! ( defined( 'WP_CLI' ) && WP_CLI ) && // Do not redirect WP-CLI commands.
				$redirect_to !== $http_host && in_array( $http_host, $redirect_from_domains, true )
			) {
			header( 'Location: https://' . $redirect_to . $request_uri, true, 301 );
			exit;
		}
	}
}

Last updated: October 13, 2021