Skip to content

Redirects in vip-config.php

Custom configurations can be added to vip-config.php to handle redirects for secondary domains to the domain currently assigned to a WordPress 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.

Requirements

For redirects to be configured for additional domains, each domains must:

Limitations

  • All characters in the domain values must be lowercase. Domains added with uppercase values will prevent the redirect code from working as expected.
  • The code examples provided here 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'].
  • 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.

Domain redirects

In this code example, requests for paths with the domains example.net or example.org will be redirected to the same path on the site www.example.com (i.e. a request to example.net/some/path will be redirected to www.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

Additional configuration is necessary for redirects between a domain’s www and non-www variants to occur on WordPress multisite environments (these are handled automatically on WordPress single site environments).

In this code example, redirects are configured for multiple domains. 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;
		}
	}
}

Last updated: August 21, 2024

Relevant to

  • WordPress