如何更新我的Laravel應用程序以在重定向時使用301
錯誤代碼而不是Laravel目前使用的默認302
?Laravel 5:使用301而不是302
例如,在我的routes.php
我使用redirect()
函數來設置錯誤代碼301
:
return redirect('URL_GOES_HERE/', 301);
但我有很多重定向在應用中發生的。有沒有辦法將所有重定向中的默認302
更改爲301
?
如何更新我的Laravel應用程序以在重定向時使用301
錯誤代碼而不是Laravel目前使用的默認302
?Laravel 5:使用301而不是302
例如,在我的routes.php
我使用redirect()
函數來設置錯誤代碼301
:
return redirect('URL_GOES_HERE/', 301);
但我有很多重定向在應用中發生的。有沒有辦法將所有重定向中的默認302
更改爲301
?
重定向定義302作爲默認值 這是一個完整的參考約Redirector.php 你可以重寫它
在這裏,你檢查的錯誤響應代碼 應用程序/例外/ 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);
}
但是'$ e-> getStatusCode()== 301'檢查響應代碼是否爲「301」?我的默認是'302'。我需要做的是將默認的'302'改爲'301'。必須使用「301」重定向的每個網址都有自己的網址,應該重定向到該網址。 – Lee
你不想使用自定義錯誤視圖? – Nour
@NinoArmani不,我需要將301錯誤代碼重定向到其他URL。 – Lee