Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Routing  ›  Lesson

Redirect Routes

Redirect Routes 12 minRoute::redirect sends visitors from one URI to another with a 302 status by default, and Route::permanentRedirect uses 301 for moves that are for good.

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 Permanently

You 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.

Three Lendly redirectsStep 1 of 4
  1. /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.

quick drill

What status does Route::redirect('/a', '/b') send by default?

quick drill

Lendly retires /members-area forever in favour of /account. Which line fits?

quick drill

Why can a mistaken 301 cause lasting trouble?

quick drill

After saving a new book, a controller should send the member to /books. Which fits?

Remember

  • Route::redirect is temporary (302); permanentRedirect is for good (301).
  • Ask "will the old URL ever come back?" When unsure, use 302.
  • Trap: a mistaken 301 lingers in browsers.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMoving a page safely

Lendly renames its /catalogue page to /books for good, and during a two-week sale it wants /offers to point at /books temporarily. Write both redirects and explain the status code each uses and why it matters.