【问题标题】:Laravel, how to redirect as 301 and 302Laravel,如何重定向为 301 和 302
【发布时间】:2013-11-26 16:43:09
【问题描述】:

我在Laravel docs 中找不到重定向为 301/302 的信息。

在我使用的 routes.php 文件中:

Route::get('foo', function(){ 
    return Redirect::to('/bar'); 
});

这是默认的 301 还是 302?有没有办法手动设置?知道为什么会从文档中省略吗?

【问题讨论】:

标签: laravel laravel-4 http-status-codes


【解决方案1】:

Laravel 301 和 302 使用 redirect() 和 route() 进行重定向

301(永久):

return redirect(route('events.show', $slug), 301);

302(临时):

默认情况下,Route::redirect 返回 302 状态码。

return redirect()->route('events.show', $slug);

官方 Laravel 文档,'重定向路由':https://laravel.com/docs/5.8/routing#redirect-routes

【讨论】:

    【解决方案2】:

    从 Laravel 5.8 开始,您可以指定 Route::redirect:

    Route::redirect('/here', '/there');
    

    默认情况下,将使用 302 HTTP 状态代码进行重定向,即临时重定向。如果页面被永久移动,您可以指定 301 HTTP 状态代码:

    Route::permanentRedirect('/here', '/there');
    /* OR */
    Route::redirect('/here', '/there', 301);
    

    Laravel 文档:https://laravel.com/docs/5.8/routing#redirect-routes

    【讨论】:

    • 仅供参考,这从 5.5 开始就在 Laravel 中
    【解决方案3】:

    您可以像这样定义直接重定向路由规则:

    Route::redirect('foo', '/bar', 301);
    

    【讨论】:

      【解决方案4】:

      martinstoeckli 的答案适用于静态网址,但对于动态网址,您可以使用以下内容。

      对于动态网址

      Route::get('foo/{id}', function($id){ 
          return Redirect::to($id, 301); 
      });
      

      现场示例(我的用例)

      Route::get('ifsc-code-of-{bank}', function($bank){ 
          return Redirect::to($bank, 301); 
      });
      

      这将重定向 http://swiftifsccode.com/ifsc-code-of-sbihttp://swiftifsccode.com/sbi

      另一个例子

      Route::get('amp/ifsc-code-of-{bank}', function($bank){ 
          return Redirect::to('amp/'.$bank, 301); 
      });
      

      这会将http://amp/swiftifsccode.com/ifsc-code-of-sbi 重定向到http://amp/swiftifsccode.com/sbi

      【讨论】:

        【解决方案5】:

        我更新了 Laravel 5 的答案! 现在你可以在文档redirect helper:

        上找到
        return redirect('/home');
        
        return redirect()->route('route.name');
        

        像往常一样.. 当你不确定的时候,你可以看看 Laravel 的 API documentation 的源代码。 Redirector class 将 $status = 302 定义为默认值(302 是临时重定向)。

        如果您希望有一个永久的 URL 重定向 (HTTP response status code 301 Moved Permanently),您可以使用 redirect() function 定义状态代码:

        Route::get('foo', function(){ 
            return redirect('/bar', 301); 
        });
        

        【讨论】:

        • 有办法使用 301 重定向到命名路由吗?可能是return redirect()->route('series')->status(301);,但我不确定。
        • 我在做类似的事情时碰巧看到了 JCarlos 的评论:在命名路由中将状态作为第三个参数传递似乎对我有用。
        • ->status(301) 对我不起作用。相反,我必须这样做 return redirect()->route('series', [], 301); 第二个数组是路由所需的任何参数,即 slug 或 id。
        • 你应该可以做到return redirect()->route('series')->setStatusCode(301);
        【解决方案6】:

        当您不确定时,您可以查看 Laravel 的 API 文档和源代码。 Redirector class$status = 302 定义为默认值。

        您可以使用to() 方法定义状态码:

        Route::get('foo', function(){ 
            return Redirect::to('/bar', 301); 
        });
        

        【讨论】:

        • 非常感谢!这很有帮助。
        • 对不起,笨蛋。我可以对Redirect::action 做同样的事情吗?
        • @Jerodev - 是的,看起来是这样,见laravel.com/api/4.2/Illuminate/Routing/…
        • 在 Laravel 5 中,您现在还可以使用简单的函数 redirect('/bar', 301)。
        • 您也可以包含路线名称,redirect(null, 301)->route('name')
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        • 2013-01-01
        • 2016-06-21
        • 2014-09-19
        相关资源
        最近更新 更多