public function onBeforeHTTPError404($request)
    {
        // We need to get the URL ourselves because $request->allParams() only has a max of 4 params
        $params = preg_split('|/+|', $request->getURL());
        $cleanURL = trim(Director::makeRelative($request->getURL(false)), '/');

        $getvars = $request->getVars();
        unset($getvars['url']);

        $page = self::find_old_page($params);
        $cleanPage = trim(Director::makeRelative($page), '/');
        if (!$cleanPage) {
            $cleanPage = Director::makeRelative(RootURLController::get_homepage_link());
        }

        if ($page && $cleanPage != $cleanURL) {
            $res = new HTTPResponse();
            $res->redirect(
                Controller::join_links(
                    $page,
                    ($getvars) ? '?' . http_build_query($getvars) : null
                ),
                301
            );
            throw new HTTPResponse_Exception($res);
        }
    }
public function onBeforeHTTPError404($request)
    {
        // We need to get the URL ourselves because $request->allParams() only has a max of 4 params
        $params = preg_split('|/+|', $request->getURL());
        $cleanURL = trim(Director::makeRelative($request->getURL(false)), '/');

        $getvars = $request->getVars();
        unset($getvars['url']);

        $page = self::find_old_page($params);
        $cleanPage = trim(Director::makeRelative($page), '/');
        if (!$cleanPage) {
            $cleanPage = Director::makeRelative(RootURLController::get_homepage_link());
        }

        if ($page && $cleanPage != $cleanURL) {
            $res = new HTTPResponse();
            $res->redirect(
                Controller::join_links(
                    $page,
                    ($getvars) ? '?' . http_build_query($getvars) : null
                ),
                301
            );
            throw new HTTPResponse_Exception($res);
        }
    }
public function onBeforeHTTPError404 ( $ request ) { // We need to get the URL ourselves because $request->allParams() only has a max of 4 params $ params = preg_split ( '|/+|' , $ request -> getURL ( ) ) ; $ cleanURL = trim ( Director :: makeRelative ( $ request -> getURL ( false ) ) , '/' ) ; $ getvars = $ request -> getVars ( ) ; unset ( $ getvars [ 'url' ] ) ; $ page = self :: find_old_page ( $ params ) ; $ cleanPage = trim ( Director :: makeRelative ( $ page ) , '/' ) ; if ( ! $ cleanPage ) { $ cleanPage = Director :: makeRelative ( RootURLController :: get_homepage_link ( ) ) ; } if ( $ page && $ cleanPage != $ cleanURL ) { $ res = new HTTPResponse ( ) ; $ res -> redirect ( Controller :: join_links ( $ page , ( $ getvars ) ? '?' . http_build_query ( $ getvars ) : null ) , 301 ) ; throw new HTTPResponse_Exception ( $ res ) ; } }
On every URL that generates a 404, we'll capture it here and see if we can
find an old URL that it should be redirecting to.

@param HTTPRequest $request The request object
@throws HTTPResponse_Exception
On every URL that generates a 404 we ll capture it here and see if we can find an old URL that it should be redirecting to .