Setting up redirects on VIP Go

VIP Go platform specific

This document is for sites running on VIP Go.

Learn more

Overview #

When you need to implement redirects involving your VIP site, you’ll want to choose one of the methods listed below based mostly upon how many redirects you have and expect to have going forward. This guide will help you figure out your best approach.

Because WordPress.com VIP uses Nginx and not apache, there aren’t any .htaccess files to modify. Instead, we have one of three methods available. Which one you use (or combination of them) depends upon how many redirects you need to implement:

  1. For a small number of redirects and redirects which will change frequently, we recommend Safe Redirect Manager plugin.
  2. For larger numbers of redirects (> 300), you will want to use the WPCOM Legacy Redirector plugin.
  3. Alternately, you can write redirects directly into your theme code.
  4. On VIP Go you can use vip-config.php for “general” redirects, e.g. to redirect all traffic from one domain to another.

If you’re using our VIP Go platform, you may be interested in the cache control of redirects; see “Controlling VIP Go Page Cache”.

↑ Top ↑

Safe Redirect Manager plugin #

If you have fewer than 300 redirects, the Safe Redirect Manager plugin is the way to go. This plugin has an admin UI, so your editors can edit redirects easily. This can be especially important for redirects that are going to change frequently. Safe Redirect Manager also supports regex, wildcards, and .PDF redirects, so if you require that, this is a good option.

Safe Redirect Manager

↑ Top ↑

WPCOM Legacy Redirector plugin #

With the WPCOM Legacy Redirector plugin, site managers can create redirects for old, or legacy, URLs which now return 404 HTTP response status codes.

There are three ways to create redirects:

  1. WP Admin — visit the Redirect Manager -> Add Redirect WP Admin screen and add redirects individually.
  2. WP-CLI and CSV — create redirects in bulk using a CSV file with the following mapping structure:
    redirect_from_path,(redirect_to_post_id|redirect_to_path|redirect_to_url)
  3. Post meta — provide the meta_key for posts where the legacy URLs are stored.

WPCOM Legacy Redirector

Be sure to review the documentation. Please contact us before using this plugin to verify your redirect strategy.

↑ Top ↑

Writing redirects directly into your theme code #

VIP has a couple of helper functions for broader redirects.

  1. vip_regex_redirects  — advanced 301 redirects using regex to match and redirect URLs. Warning: Since regex is expensive and this will be run on every uncached page load, you’ll want to keep this small, lean, and mean.
  2. vip_substr_redirects — wildcard redirects based on the beginning of the request path. This is basically an alternative to vip_regex_redirects() for when you only need to redirect /foo/bar/* to somewhere else. Using regex to do this simple check would add lots of overhead.

↑ Top ↑

VIP Go: Domain redirects in vip-config.php #

This method will only work on our VIP Go platform. 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, please 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 which 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'] ) ) {
	$http_host   = $_SERVER['HTTP_HOST'];
	$request_uri = $_SERVER['REQUEST_URI'];

	$redirect_to_domain = 'www.example.com';
	$redirect_domains   = [
		'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 && // safety
			! ( defined( 'WP_CLI' ) && WP_CLI ) && // safety
			$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'] ) ) {
	$http_host   = $_SERVER['HTTP_HOST'];
	$request_uri = $_SERVER['REQUEST_URI'];

	$redirect_domains = [
		/*
		'to-this-domain.com' => [
			'from-this-domain.com',
			'or-from-this-domain.com',
		],
		*/
		'example-one.com'   => [
			'www.example-one.com',
			'www.exampleone.com',
		],
		'example-two.com'   => [
			'www.example-two.com',
			'www.exampletwo.com',
		],
		'example-three.com' => [
			'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 && // safety
				! ( defined( 'WP_CLI' ) && WP_CLI ) && // safety
				$redirect_to !== $http_host && in_array( $http_host, $redirect_from_domains, true )
			) {
			header( 'Location: https://' . $redirect_to . $request_uri, true, 301 );
			exit;
		}
	}
}

 

↑ Top ↑

Additional Resources #

Ready to get started?

Drop us a note.

No matter where you are in the planning process, we’re happy to help, and we’re actual humans here on the other side of the form. 👋 We’re here to discuss your challenges and plans, evaluate your existing resources or a potential partner, or even make some initial recommendations. And, of course, we’re here to help any time you’re in the market for some robust WordPress awesomeness.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.