如何更新我的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