Laravel,如何重定向为 301 和 302
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19964663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel, how to redirect as 301 and 302
提问by Justin
I cannot find info for redirecting as 301/302 in the Laravel docs.
我在Laravel 文档中找不到重定向为 301/302 的信息。
In my routes.php file I use:
在我的 routes.php 文件中,我使用:
Route::get('foo', function(){
return Redirect::to('/bar');
});
Is this a 301 or 302 by default? Is there a way to set it manually? Any idea why this would be omitted from the docs?
这是默认的 301 还是 302?有没有办法手动设置?知道为什么这会从文档中省略吗?
回答by martinstoeckli
Whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector classdefines a $status = 302
as default value.
如果您不确定,可以查看 Laravel 的 API 文档和源代码。该重定向器类定义$status = 302
为默认值。
You can define the status code with the to()
method:
您可以使用以下to()
方法定义状态代码:
Route::get('foo', function(){
return Redirect::to('/bar', 301);
});
回答by Luca Mori Polpettini
I update the answer for Laravel 5! Now you can find on docs redirect helper:
我更新了 Laravel 5 的答案!现在您可以在文档重定向助手上找到:
return redirect('/home');
return redirect()->route('route.name');
As usual.. whenever you are unsure, you can have a look at Laravel's API documentationwith the source code. The Redirector classdefines a $status = 302 as default value (302 is a temporary redirect).
像往常一样......当你不确定时,你可以查看带有源代码的Laravel 的API 文档。该重定向器类定义了一个$状态= 302作为默认值(302是临时重定向)。
If you wish have a permanent URL redirection (HTTP response status code 301 Moved Permanently), you can define the status code with the redirect() function:
如果您希望有一个永久的 URL 重定向(HTTP 响应状态代码 301 永久移动),您可以使用redirect() 函数定义状态代码:
Route::get('foo', function(){
return redirect('/bar', 301);
});
回答by Abhishek Goel
martinstoeckli's answer is good for static urls, but for dynmaic urls you can use the following.
martinstoeckli 的答案适用于静态网址,但对于动态网址,您可以使用以下内容。
For Dynamic URLs
对于动态 URL
Route::get('foo/{id}', function($id){
return Redirect::to($id, 301);
});
Live Example (my use case)
实时示例(我的用例)
Route::get('ifsc-code-of-{bank}', function($bank){
return Redirect::to($bank, 301);
});
This will redirect http://swiftifsccode.com/ifsc-code-of-sbito http://swiftifsccode.com/sbi
这会将http://swiftifsccode.com/ifsc-code-of-sbi重定向 到http://swiftifsccode.com/sbi
One more Example
再举一个例子
Route::get('amp/ifsc-code-of-{bank}', function($bank){
return Redirect::to('amp/'.$bank, 301);
});
This will redirect http://amp/swiftifsccode.com/ifsc-code-of-sbito http://amp/swiftifsccode.com/sbi
这会将http://amp/swiftifsccode.com/ifsc-code-of-sbi重定向到http://amp/swiftifsccode.com/sbi
回答by StR
You can define a direct redirect route rule like this:
您可以像这样定义直接重定向路由规则:
Route::redirect('foo', '/bar', 301);
回答by Connor Leech
As of Laravel 5.8 you can specify Route::redirect
:
从 Laravel 5.8 开始,您可以指定Route::redirect
:
Route::redirect('/here', '/there');
By default that will redirect with 302 HTTP status code, meaning temporary redirect. If the page is permanently moved you can specify 301 HTTP status code:
默认情况下,它将使用 302 HTTP 状态代码重定向,这意味着临时重定向。如果页面被永久移动,您可以指定 301 HTTP 状态代码:
Route::permanentRedirect('/here', '/there');
/* OR */
Route::redirect('/here', '/there', 301);
Laravel docs: https://laravel.com/docs/5.8/routing#redirect-routes
Laravel 文档:https://laravel.com/docs/5.8/routing#redirect-routes
回答by Darren Murphy
Laravel 301 and 302 redirects using redirect() and route()
Laravel 301 和 302 使用 redirect() 和 route() 重定向
301 (permanent):
301(永久):
return redirect(route('events.show', $slug), 301);
302 (temporary):
302(临时):
By default, Route::redirect returns a 302 status code.
默认情况下,Route::redirect 返回 302 状态代码。
return redirect()->route('events.show', $slug);
Offical Laravel Docs,'Redirect Routes': https://laravel.com/docs/5.8/routing#redirect-routes
官方 Laravel 文档,“重定向路由”:https://laravel.com/docs/5.8/routing#redirect-routes