Redirect, View, and Fallback Routes

+15 Mana ✨

Introduction

Not every route needs a controller. Laravel ships with three shortcuts for the boring cases: Route::redirect for permanent URL moves, Route::view for a static Blade template, and Route::fallback for anything that does not match.

Key Concepts

  • Route::redirect(from, to, status?): Permanently or temporarily redirects one URL to another.
  • Route::view(path, template, data?): Renders a Blade view directly without a controller.
  • Route::fallback(callback): Handles any URL no other route matched — your custom 404.

Real World Context

When you rebuild a site, old URLs need to survive for SEO. A one-liner Route::redirect('/old-blog', '/blog', 301) beats setting up nginx rewrites. Route::view is perfect for marketing pages that never touch a database.

Deep Dive

Redirect Routes

php
// Default status is 302 (temporary)
Route::redirect('/here', '/there');

// Explicit status
Route::redirect('/here', '/there', 301);

// Permanent redirect helper
Route::permanentRedirect('/old', '/new');

301s tell search engines the move is permanent and to update their index; 302s are temporary.

View Routes

php
// Render a view with no data
Route::view('/welcome', 'welcome');

// Pass static data to the view
Route::view('/about', 'about', [
    'team' => 'Engineering',
]);

For routes that only render a template, Route::view saves you a one-line controller. When you eventually need dynamic data, upgrade to a controller.

Fallback Route

php
// Always define this LAST in routes/web.php
Route::fallback(function () {
    return response()->view('errors.custom-404', [], 404);
});

The fallback catches any request that did not match another route. Laravel already renders resources/views/errors/404.blade.php if it exists, so use the fallback route when you want a dynamic 404 page with recommended links, a search box, or logging.

Common Pitfalls

  1. Defining the fallback too early — The fallback must be the LAST registered route or it will swallow later definitions. Put it at the bottom of routes/web.php.
  2. Using 302 for a permanent move — Search engines will keep the old URL in their index. Use 301 when the move is permanent.

Best Practices

  1. Prefer permanentRedirect for SEO — Its intent is explicit and it always emits 301.
  2. Log fallback hits — A sudden spike of 404s often means a typo in your latest deploy. Log them from the fallback route so you notice quickly.

Summary

  • Route::redirect replaces hand-rolled controllers for URL moves.
  • Route::view renders a Blade template without a controller.
  • Route::fallback handles unmatched URLs — define it last.
  • Use 301 for permanent SEO-friendly redirects, 302 for temporary ones.

Code Examples

php
// routes/web.php
use Illuminate\Support\Facades\Route;

Route::permanentRedirect('/old-blog', '/blog');

Route::view('/welcome', 'welcome');
Route::view('/about', 'about', ['team' => 'Engineering']);

// Must be the last route in the file
Route::fallback(function () {
    return response()->view('errors.not-found', [],  404);
});
✓ Completed