Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, how to redirect as 301 and 302

I cannot find info for redirecting as 301/302 in the Laravel docs.

In my routes.php file I use:

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?

like image 588
Justin Avatar asked Nov 13 '13 21:11

Justin


People also ask

What is a 301 and 302 redirect?

The user's search experience may be the same as both options land the user on the appropriate webpage. However, search engines handle these types of URL redirects differently - the 302 redirect means that the page has been moved temporarily and other, 301, means that a new page has taken over permanently.


1 Answers

Whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector class defines a $status = 302 as default value.

You can define the status code with the to() method:

Route::get('foo', function(){      return Redirect::to('/bar', 301);  }); 
like image 139
martinstoeckli Avatar answered Oct 08 '22 01:10

martinstoeckli