Friday, April 14, 2017

Laravel redirect old links

I have an issue: need to redirect many broken links to the new destination. For the moment all old links are pointing to the default 404 page, but that is not an option.

I want to create a Controller in which i will define the rules for redirection.

Unfortunately I do not know how to catch all broken links to point to my new Controller.

I have made the corrections you have suggest, but unfortunately after redirect i am getting Error 500. the $redirect_url is correct, i have checked it.

Here is the new Model function:

public function redirect($url) {

    $redirect_url = '';
    if(preg_match('!/bukmeikari/(.*)/(.*).html!isU', $url, $matches)) {
        $redirect_url = str_replace('/bukmeikari/', '/bg/bukmeikari/', $url);
    }
    if($redirect_url != '') {
        Redirect::to($redirect_url, 301);
    }
}

and in the Handler.php:

public function render($request, Exception $e)
{
    $rewrite_url = new RewriteUrl();
    $rewrite_url->redirect(request()->fullUrl());

    if($e instanceof NotFoundHttpException) {
        return response()->view('errors.404', [], 404);
    }
    return parent::render($request, $e);
}

Thanks in advance



via Vince Carter

Advertisement