Skip to the workspace
Pebellslearn by building
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.

laravel / Redirect Routes

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.