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.
The shortcut
Ask "will the old URL ever come back?" If it might, use Route::redirect (302). If it never will, use Route::permanentRedirect (301). When unsure, choose 302. A wrong 302 is harmless, but a wrong 301 can stick in visitors' browsers.
/catalogue to /books
The old name is retired forever.
Route::permanentRedirect('/catalogue', '/books').
The trap is using a 301 "because it is the proper one" for something temporary. Browsers may keep jumping to the old target long after you have changed the route.
Drills
Answer fast — trust the rule.
What status does Route::redirect('/a', '/b') send by default?
Lendly retires /members-area forever in favour of /account. Which line fits?
Why can a mistaken 301 cause lasting trouble?
After saving a new book, a controller should send the member to /books. Which fits?
Remember
Route::redirectis temporary (302);permanentRedirectis for good (301).- Ask "will the old URL ever come back?" When unsure, use 302.
- Trap: a mistaken 301 lingers in browsers.
