protected function createRedirect($data)
 {
     $redirect = OldURLRedirect::create();
     $redirect->update($data);
     $redirect->write();
     return $redirect;
 }
 /**
  * @param SS_HTTPRequest $request
  * @throws SS_HTTPResponse_Exception
  */
 public function onBeforeHTTPError404($request)
 {
     $url = strtolower($this->getUrl($request));
     $oldPage = OldURLRedirect::get_from_url($url);
     // If there's a match, direct!
     if ($oldPage) {
         $response = new SS_HTTPResponse();
         $dest = $oldPage->getRedirectionLink();
         $response->redirect(Director::absoluteURL($dest), $oldPage->getRedirectCode());
         throw new SS_HTTPResponse_Exception($response);
     }
 }
    /**
     * Lookup an OldURLRedirect page which matches the url
     *
     * @param $url
     * @return DataObject|null
     */
    public static function get_from_url($url)
    {
        $url = $url ? $url : (!empty($_GET['url']) ? $_GET['url'] : '');
        if ($url) {
            if (strpos($url, '/') !== 0) {
                $url = '/' . $url;
            }
            $SQL_url = Convert::raw2sql($url);
            $filter = <<<SQL
("OldURL" = '{$SQL_url}' AND "RedirectType" = 'Custom')
OR
("OldURL" = '{$SQL_url}' AND "RedirectType" = 'Internal' AND "PageID" <> 0)
SQL;
            $oldPage = OldURLRedirect::get()->where($filter)->first();
            if ($oldPage && $url == strtolower($oldPage->OldURL)) {
                return $oldPage;
            }
        }
        return null;
    }