Pantheon Community

Bulk Redirection via JSON file

I wanted to repost a solution from Slack for redirection that works well and isn’t included in the Pantheon documents regarding redirection. The included examples are great for dealing with items individually or in small groups. But the referenced code doesn’t scale too well when you need to capture multiple redirects and place them within your code base.

The solution consists of two files. The first is included (directly or via include_once) within wp-config.php. The second file is a JSON file which actually contains the redirects.

The script works like this:

  • It processes the list of the single page redirects, looking for an exact match.
  • If that match isn’t found, it processes a general “fallback” rule for the redirected domain.

Included script:

// PHP snippet included in wp-config.php (or settings.php).
// Including from that location in a separate file is OK as well.

<?php
// Remove any leading "www." from the host name.
$redirect_host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
$redirect_path = strtolower(rtrim($_SERVER['REQUEST_URI']));
if (strlen($redirect_path) > 2) {
  $redirect_path = rtrim($redirect_path, '/');
}
$redirect_search = strtolower(($redirect_host . $redirect_path));
$redirects_json = dirname(__FILE__) . '/files/private/redirects/redirects.json';
if (file_exists($redirects_json)) {
  $redirect_patterns = json_decode(file_get_contents($redirects_json), TRUE);
  // Exact match redirects
  if (in_array($redirect_search, array_keys($redirect_patterns['singles']))) {
    $redirect_to = $redirect_patterns['singles'][$redirect_search];
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: ' . $redirect_to);
    exit();
  }
  // Domain based redirects.
  if (in_array($redirect_host, array_keys($redirect_patterns['domains']))) {
    $redirect_to = $redirect_patterns['domains'][$redirect_host];
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: ' . $redirect_to);
    exit();
  }
}

JSON Example

{
  "singles": {
    "example.com/page1": "https://example.net/new-page-1",
    "example.com/page2": "https://example.net/new-page-2"
  },
  "domains": {
    "example.org": "https://example.net/all-from-example-org",
    "example.foo": "https://example.net/redirect-from-example-foo"
  }
}

Many, many thanks to the original poster of the script david@davidlanier.com. I also captured it as a Gist here: https://gist.github.com/SteveRyan-ASU/46586c45b190895fbf0ae109d03f9640

1 Like

Adding some additional notes here:

  • Untested (personally), but this was also referenced as a solution for Drupal sites as well as WP sites. Include the code within settings.php and it should work the same way.
  • The JSON file includes keys that are pretty specific regarding formatting. I learned the hard way that the script fails silently if you happen to include an http:// in one of the URLs to be redirected. (Fully qualified links as the redirection targets are totally OK.)

Also: I found it annoyingly difficult to test each one of the links within the script. But, I bumped into this article and Google Sheet for a very low-tech monitoring solution that works well. https://www.thetechseo.com/seo-tools/redirect-checker/

Happy redirecting!