Skip to content

Domain redirects in vip-config.php

To use more than one domain per site, custom configurations can be added to vip-config.php to handle redirecting secondary domains to the main domain for a site.

Handling domain redirects in vip-config.php improves the speed and performance of the redirects because vip-config.php is loaded before WordPress. However, because this code runs before WordPress is loaded, WordPress APIs and functions cannot be relied upon, and the $_SERVER global variable has not been normalized with default values.

In this code example, requests for example.net or example.org will be redirected to the same path on the main site, example.com (i.e., a request to example.net/some/path will be redirected to example.com/some/path).

vip-config/vip-config.php
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;
	}
}

Redirects between www and non-www

On WordPress single sites, redirects between a domain’s www and non-www variants are handled automatically. On WordPress multisites, additional configuration is necessary for redirects between a domain’s www and non-www variants to occur.

In this code example, redirects for multiple domains are configured. Each example shows www variants of a domain redirecting to the domain’s non-www variant. The example also shows how to include redirects for other domains in addition to the the www and non-www variants. The structure of this redirect code: A => [B, C], redirects B and C to A.

vip-config/vip-config.php
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.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;
		}
	}
}

Limitations

  • When adding redirects to vip-config.php, be sure to exclude the /cache-healthcheck? URI from the redirects. This URI is used by the VIP Platform to check the health of a site, and a redirect response will cause issues with the site being served correctly.
  • The code examples above will only handle redirects for fully qualified domain names (FQDNs), however they could be modified to handle redirects for specific URIs (e.g., example.com/blog/) by checking the value of $_SERVER['REQUEST_URI'].
  • All characters in the domain values must be lowercase. Domains added with uppercase values will prevent the redirect code from working as expected.

Last updated: December 22, 2023

Relevant to

  • WordPress