Skip to content

Instantly share code, notes, and snippets.

@nathanbrauer
Last active October 18, 2017 20:27
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 nathanbrauer/9b06292c0fa587629c7ebb0dd7f1ffac to your computer and use it in GitHub Desktop.
Save nathanbrauer/9b06292c0fa587629c7ebb0dd7f1ffac to your computer and use it in GitHub Desktop.
<?php
class RedirectController extends Controller
{
private static $allowed_actions = [
];
public function init()
{
parent::init();
$host = $this->request->getHeader('Host');
$path = $_SERVER['REQUEST_URI'];
$redirects = Redirect::get_by_url($host, $path)->sort('Priority DESC, ID ASC');
if ($redirects->count()) {
foreach ($redirects as $redirect) {
/** @var Redirect $redirect */
if ($redirect->matchesParams($_GET)) {
$redirectLink = $redirect->replaceRequest($host . $path);
// SS can not handle 308 Permanent Redirect. See SS_HTTPResponse
if ($redirect->ResponseCode == 308) {
header('Location: ' . $redirectLink, null, 308);
exit;
} else {
return $this->redirect($redirectLink, $redirect->ResponseCode);
}
}
}
}
return $this->httpError(404, "Nowhere to go.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment