MasterPHP.in
Try Compiler →
Laravel Tutorial

Redirect, View & Fallback Routes in Laravel


Every real website has a few routes that do almost nothing. Your invoice app might have an old /bills page that people bookmarked before you renamed it to /invoices. It will have static pages like Pricing and Terms, where there is no logic at all, only a template to show. And sooner or later, someone will type a wrong URL and land on a plain, unfriendly error page.

You could write a full closure or a controller for each of these, but that is a lot of ceremony for very little work. Laravel has three small helpers built for exactly these jobs: redirect routes, view routes and fallback routes. Each one replaces several lines of code with one, and together they finish off the routing chapter.

 

Redirect Routes

A redirect route sends the visitor from one URL to another, with no controller and no closure.

 

The Basic Redirect

Suppose your client renamed /bills to /invoices. Anyone with the old link should still reach the right page.

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

Route::redirect('/bills', '/invoices');

The first argument is the old URL and the second is where to send the visitor. By default, Laravel sends a 302 status code, which means “temporarily moved.”

 

Permanent Redirects (301)

If the move is permanent, tell browsers and search engines by sending a 301 status. Pass it as the third argument, or use the dedicated method:

Route::redirect('/bills', '/invoices', 301);

// Same thing
Route::permanentRedirect('/bills', '/invoices');

A 301 tells Google to replace the old URL with the new one in its index, so your search ranking moves with it. One warning: browsers remember 301 redirects very strongly. While you are still testing, use the default 302, and switch to 301 only when you are sure.

 

Redirects with Parameters

Redirect routes can carry route parameters, just like normal routes. This is useful when old URLs included an id:

Route::redirect('/bills/{id}', '/invoices/{id}', 301);

A visit to /bills/1042 now lands on /invoices/1042. Laravel reserves two names for its own use in redirect routes, destination and status, so do not use those as parameter names.

 

Route::redirect vs the redirect() Helper

These two look similar but are used in different places:

  • Route::redirect() goes in routes/web.php and handles a whole URL that should always move somewhere else
  • redirect()->route() goes inside a controller or closure and sends the user onward after an action, such as saving an invoice

You already used redirect()->route() in the Named Routes lesson. Redirects after form submissions, with flash messages, are covered in the Redirects & Flash Session Data lesson.

 

View Routes

Many pages are only a template. An About page, a Terms page, a Pricing page. Writing a closure for each one looks like this:

// Works, but more code than needed
Route::get('/terms', function () {
    return view('legal.terms');
});

Route::view() does the same job in one line:

Route::view('/terms', 'legal.terms');

The first argument is the URL and the second is the view name. The name legal.terms points to the file resources/views/legal/terms.blade.php. Even the default home route in a fresh Laravel project can be shortened this way:

// Before
Route::get('/', function () {
    return view('welcome');
});

// After
Route::view('/', 'welcome');

 

Passing Data to the View

You can pass an array of data as the third argument. Here is a pricing page that shows the GST rate:

Route::view('/pricing', 'pricing', ['gstRate' => 18])->name('pricing');


{{-- resources/views/pricing.blade.php --}}
<p>All prices are exclusive of {{ $gstRate }}% GST.</p>

Notice the ->name('pricing') at the end. View routes can be named like any other route, so route('pricing') works in your links. The names view, data, status and headers are reserved by Laravel, so avoid them as route parameter names.

 

When Not to Use a View Route

A view route is for pages that need no logic. The moment a page needs to read from the database, check who is logged in, or decide what to show, move it to a controller. You will start building controllers in the next chapter.

// Good fit: static content
Route::view('/terms', 'legal.terms');

// Bad fit: needs database data, so use a controller instead
Route::view('/invoices', 'invoices.index');

 

Fallback Routes

What happens when a visitor opens /invoise with a typo? No route matches, so Laravel shows its default 404 page. A fallback route lets you take control of that moment and decide what to show.

 

A Basic Fallback Route

Route::fallback(function () {
    return response()->view('errors.not-found', [], 404);
});

This route runs only when no other route matched the request. It can show a friendly page that suggests links to the dashboard or the invoice list. Laravel’s documentation recommends registering the fallback as the last route in your file, so keep it at the very bottom of routes/web.php.

The 404 in the code matters. The third argument of response()->view() is the status code. Without it, the page would be sent with a 200 “OK” status even though nothing was found. Search engines would then treat your error page as a real page.

 

Fallback for APIs

Mobile apps and JavaScript frontends expect JSON, not HTML. A fallback in your API routes should return JSON with a 404 status:

Route::fallback(function () {
    return response()->json(['message' => 'Route not found.'], 404);
});

The routes/api.php file does not exist in a fresh project. You will add it in the Building REST APIs chapter, where API error handling is covered properly.

 

What a Fallback Does Not Catch

A fallback route only handles URLs that match no route at all. It does not run when a route matched but your code decided the record does not exist. For example, /invoices/99999 matches the invoices.show route, so a missing invoice is handled by Laravel’s error handling, not by your fallback. Custom error pages for those cases are covered in the Error Handling & Custom Error Pages lesson.

 

Putting It All Together

Here is how a small routes/web.php for the invoice app might look with everything from this chapter in place:

use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;

// Static pages
Route::view('/', 'welcome')->name('home');
Route::view('/pricing', 'pricing', ['gstRate' => 18])->name('pricing');
Route::view('/terms', 'legal.terms')->name('terms');

// Old URLs that moved permanently
Route::redirect('/bills', '/invoices', 301);
Route::redirect('/bills/{id}', '/invoices/{id}', 301);

// Main feature
Route::resource('invoices', InvoiceController::class);

// Anything else
Route::fallback(function () {
    return response()->view('errors.not-found', [], 404);
});

Static pages at the top, redirects next, the real application routes in the middle and the fallback last. Run php artisan route:list and you will see every one of them listed.

 

Common Mistakes

 

Mistake 1: Creating a Redirect Loop

Route::redirect('/bills', '/invoices');
Route::redirect('/invoices', '/bills');


This page isn't working
localhost redirected you too many times.
ERR_TOO_MANY_REDIRECTS

Each URL sends the visitor to the other, forever. Check that your redirect destination is a real page that does not redirect back.

 

Mistake 2: Using 301 Too Early

You set Route::redirect('/offers', '/pricing', 301) while testing, then change your mind and remove the redirect. Your browser still jumps to /pricing, because it cached the 301. Use the default 302 while you are experimenting, and switch to 301 only for moves that are final.

 

Mistake 3: Pointing a View Route at a Missing View

Route::view('/terms', 'legal.terms');


InvalidArgumentException
View [legal.terms] not found.

The view file does not exist at resources/views/legal/terms.blade.php. Check the folder and file name. Dots in the view name stand for folder separators.

 

Mistake 4: Forgetting the 404 Status in a Fallback

// Wrong: this page is sent with status 200
Route::fallback(function () {
    return view('errors.not-found');
});

The visitor sees a “not found” message, but the server says everything is fine. Always send the status code: response()->view('errors.not-found', [], 404).

 

Mistake 4: Putting Logic in a View Route

Trying to squeeze database queries or login checks into a view route leads to messy code. If a page needs anything more than a template and a few fixed values, use a controller.

 

Quick Summary

  • Route::redirect('/old', '/new') redirects a whole URL with no controller, and sends a 302 by default
  • Use Route::permanentRedirect() or a third argument of 301 for moves that are final
  • Redirect routes accept parameters like /bills/{id}, but destination and status are reserved names
  • Route::redirect() is for whole URLs, and redirect()->route() is for sending users onward after an action
  • Route::view('/terms', 'legal.terms') shows a template with no closure, and a third argument passes data
  • View routes can be named, and are best for pages without logic
  • Route::fallback() handles every URL that matched no route, so keep it at the bottom of the file
  • Always return a 404 status from a fallback, and return JSON in API routes
  • A fallback does not handle missing records inside a matched route

 

What’s Next

You have now covered every part of routing in Laravel: basic routes, parameters, names, groups, model binding, resources and the special route types from this lesson. Routes only decide where a request goes, though, and the real work happens after that. In the next chapter, starting with Creating Your First Controller, you will move your logic out of routes and into controller classes.

Frequently Asked Questions

Route::redirect is used in your routes file to send one whole URL to another, with no extra code. The redirect() helper is used inside a controller or closure to send a user somewhere after an action, such as saving a form. Use the first for moved pages and the second for normal application flow.
Use 301 when a URL has moved for good, because search engines then pass the old page’s ranking to the new one. Use 302 for temporary moves, such as a short maintenance page. If you are unsure, start with 302, since browsers cache a 301 for a long time and it is hard to undo.
Use Route::view for pages that only show a template, such as About, Terms or Pricing, where nothing needs to be calculated or loaded from a database. As soon as the page needs logic, user data or database records, switch to a controller.
A fallback route handles every request that matches no other route. You do not strictly need one, because Laravel already shows a default 404 page. Add one when you want a branded error page for unknown URLs or a JSON message for an API.
For URLs that match no route, register a fallback route that returns your own view with a 404 status. For other 404 cases, such as a missing record, Laravel can also use a custom error page placed in the errors folder inside resources/views. That second approach is covered in the error handling chapter.

Share this tutorial