Created
January 31, 2018 17:14
-
-
Save SteveRyan-ASU/46586c45b190895fbf0ae109d03f9640 to your computer and use it in GitHub Desktop.
Pantheon: Redirect via JSON feed in private file section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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
Thank you to David Lanier (via Pantheon Slack channel) for this snippet.
You might want to contact him for more... [email protected]