Redirect Routes
Lendly's first version called the book list /catalogue. Now it lives at /books, but old bookmarks and search results still point at the old address. You do not want them to hit a 404. A redirect route forwards them in one line.
The rule
Route::redirect is temporary (302); permanentRedirect is for good (301).
Route::redirect('/offers', '/books'); // 302 Found
Route::permanentRedirect('/catalogue', '/books'); // 301 Moved PermanentlyYou can also pass the status yourself:
Route::redirect('/catalogue', '/books', 301);The status code is the point of the lesson:
- 302 (temporary): "It's over there for now." Browsers do not remember it, and search engines keep the old URL listed.
- 301 (permanent): "It has moved for good." Browsers may cache the redirect and go straight to the new address next time, and search engines move the old page's ranking to the new one.
A redirect route needs no controller or closure. It is a finished route, so you can still name it or add middleware.
Redirects inside your own code look different. A closure or controller that ends with return redirect('/books'); or return redirect()->route('books.index'); is redirecting after doing some work, such as saving a form. Route::redirect is for when there is no work at all, just an old address pointing at a new one.