2016-09-20 30 views
3

如何更新我的Laravel應用程序以在重定向時使用301錯誤代碼而不是Laravel目前使用的默認302Laravel 5:使用301而不是302

例如,在我的routes.php我使用redirect()函數來設置錯誤代碼301

return redirect('URL_GOES_HERE/', 301);

但我有很多重定向在應用中發生的。有沒有辦法將所有重定向中的默認302更改爲301

+0

你不想使用自定義錯誤視圖? – Nour

+0

@NinoArmani不,我需要將301錯誤代碼重定向到其他URL。 – Lee

回答

0

重定向定義302作爲默認值 這是一個完整的參考約Redirector.php 你可以重寫它

0

在這裏,你檢查的錯誤響應代碼 應用程序/例外/ Handler.php

public function render($request, Exception $e) 
{ 
    // If response code 404 then do this 
    if($e->getStatusCode() == 404) 
    { 
    return redirect()->to('/error'); 
    } 

    // If response code 301 
    if($e->getStatusCode() == 301) 
    { 
    return redirect()->to('/do'); 
    } 

    if ($e instanceof ModelNotFoundException) { 
    $e = new NotFoundHttpException($e->getMessage(), $e); 
    } 

    if ($e instanceof \Illuminate\Session\TokenMismatchException) { 
    return redirect('/')->with('message', 'Sorry, your session seems to have expired. Please login again.'); 
    } 

    return parent::render($request, $e); 
} 
+0

但是'$ e-> getStatusCode()== 301'檢查響應代碼是否爲「301」?我的默認是'302'。我需要做的是將默認的'302'改爲'301'。必須使用「301」重定向的每個網址都有自己的網址,應該重定向到該網址。 – Lee