Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtschlatzer/a367d3f7b52372666ea8ec8a25a8ff12 to your computer and use it in GitHub Desktop.
Save kurtschlatzer/a367d3f7b52372666ea8ec8a25a8ff12 to your computer and use it in GitHub Desktop.
Pantheon: Redirect via JSON feed in private file section
// 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();
}
}
{
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment