-
-
Save nathanbrauer/9b06292c0fa587629c7ebb0dd7f1ffac to your computer and use it in GitHub Desktop.
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 | |
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