Laravel send to 301 page | How to redirect as 301 in Laravel | How To Handle Redirects In Laravel | Laravel redirect code 301 | permanent redirection

In Laravel, you can perform a 301 (permanent) redirect using the redirect method with the with() method chain. Here's how you can set up a 301 redirect in Laravel:

php
return redirect()->away('https://example.com', 301);

In the code above, we use the redirect() method to create a redirect response, and then we use the away() method to specify the target URL for the redirect. The 301 parameter indicates that it's a permanent redirect.

You can replace 'https://example.com' with the URL you want to redirect to.

If you want to perform a 301 redirect within a specific route or controller method, you can do so as follows:

php
use Illuminate\Http\RedirectResponse; public function yourMethod() { return new RedirectResponse('https://example.com', 301); }

In this example, we use the RedirectResponse class to create a 301 redirect response.

Remember to import the necessary classes at the top of your controller or route file:

php
use Illuminate\Http\RedirectResponse;

This will ensure that Laravel handles the redirect with a 301 status code, indicating that it's a permanent redirection.

Comments