Laravel 5:使用301而不是302

4
我该如何更新我的Laravel应用程序,以便在重定向时使用301错误代码而不是Laravel当前使用的默认302
例如,在我的routes.php中,我使用redirect()函数设置错误代码301return redirect('URL_GOES_HERE/', 301); 但是我的应用程序中有许多重定向。有没有一种方法可以简单地将所有重定向的默认值从302更改为301

你不想使用自定义错误视图吗? - Nour
@NinoArmani 不,我需要使用301错误代码将其重定向到其他URL。 - Max
2个回答

0

将302定义为默认值的重定向器, 这是有关Redirector.php的完整参考资料 您可以覆盖它


-1

在这里可以检查错误响应代码 app/Exceptions/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重定向的URL都有自己的URL,应该将其重定向到该URL。 - Max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接