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?
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:I update the answer for Laravel 5! Now you can find on docs redirect helper:
As usual.. 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 (302 is a temporary redirect).
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:
You can define a direct redirect route rule like this:
martinstoeckli's answer is good for static urls, but for dynmaic urls you can use the following.
For Dynamic URLs
Live Example (my use case)
This will redirect http://swiftifsccode.com/ifsc-code-of-sbi to http://swiftifsccode.com/sbi
One more Example
This will redirect http://amp/swiftifsccode.com/ifsc-code-of-sbi to http://amp/swiftifsccode.com/sbi