PHPackages                             spatie/laravel-missing-page-redirector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/laravel-missing-page-redirector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/laravel-missing-page-redirector
======================================

Redirect missing pages in your Laravel application

2.12.0(2mo ago)5071.4M—5%3513MITPHPPHP ^8.0|^8.1CI passing

Since Oct 13Pushed 2mo ago5 watchersCompare

[ Source](https://github.com/spatie/laravel-missing-page-redirector)[ Packagist](https://packagist.org/packages/spatie/laravel-missing-page-redirector)[ Docs](https://github.com/spatie/laravel-missing-page-redirector)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-laravel-missing-page-redirector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (32)Used By (13)

Redirect missing pages in your Laravel application
==================================================

[](#redirect-missing-pages-in-your-laravel-application)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f71dfd1741cbb7e7d8cbcc012111699a05fb6a40de623594e3e446af9b66ead1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d6d697373696e672d706167652d72656469726563746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-missing-page-redirector)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![run-tests](https://github.com/spatie/laravel-missing-page-redirector/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-missing-page-redirector/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/f91591bf37b4ac66f195aa8d31dea9b8ba7d1235c1d7f7857e41ed132158fb9c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d6d697373696e672d706167652d72656469726563746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-missing-page-redirector)

When transitioning from a old site to a new one your URLs may change. If your old site was popular you probably want to retain your SEO worth. One way of doing this is by providing [permanent redirects from your old URLs to your new URLs](https://support.google.com/webmasters/answer/93633?hl=en). This package makes that process very easy.

When installed you only need to [add your redirects to the config file](https://github.com/spatie/laravel-missing-page-redirector#usage). Want to use the database as your source of redirects? [No problem](https://github.com/spatie/laravel-missing-page-redirector#creating-your-own-redirector)!

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/27dc0dc36b26b9a601d5f1ac9e77eb56e9926380155966c0aaf1160896e3b33f/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d6d697373696e672d706167652d72656469726563746f722e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-missing-page-redirector)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-missing-page-redirector
```

The package will automatically register itself.

Next, prepend/append the `Spatie\MissingPageRedirector\RedirectsMissingPages` middleware to your global middleware stack:

```
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->append([
        \Spatie\MissingPageRedirector\RedirectsMissingPages::class,
    ]);
})
```

Finally you must publish the config file:

```
php artisan vendor:publish --provider="Spatie\MissingPageRedirector\MissingPageRedirectorServiceProvider"
```

This is the contents of the published config file:

```
return [
    /*
     * This is the class responsible for providing the URLs which must be redirected.
     * The only requirement for the redirector is that it needs to implement the
     * `Spatie\MissingPageRedirector\Redirector\Redirector`-interface
     */
    'redirector' => \Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector::class,

    /*
     * By default the package will only redirect 404s. If you want to redirect on other
     * response codes, just add them to the array. Leave the array empty to redirect
     * always no matter what the response code.
     */
    'redirect_status_codes' => [
        \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND
    ],

    /*
     * When using the `ConfigurationRedirector` you can specify the redirects in this array.
     * You can use Laravel's route parameters here.
     */
    'redirects' => [
//        '/non-existing-page' => '/existing-page',
//        '/old-blog/{url}' => '/new-blog/{url}',
    ],

];
```

Usage
-----

[](#usage)

Creating a redirect is easy. You just have to add an entry to the `redirects` key in the config file.

```
'redirects' => [
   '/non-existing-page' => '/existing-page',
],
```

You may use route parameters like you're used to when using Laravel's routes:

```
    'redirects' => [
       '/old-blog/{url}' => '/new-blog/{url}',
    ],
```

Optional parameters are also... an option:

```
    'redirects' => [
       '/old-blog/{url?}' => '/new-blog/{url}',
    ],
```

Finally, you can use an asterix (`*`) as a wildcard parameter that will match multiple URL segments (see [encoded URL slashes in the Laravel docs](https://laravel.com/docs/master/routing#parameters-encoded-forward-slashes) for more info). This is useful when you want to redirect a URL like `/old-blog/foo/bar/baz` to `/new-blog/foo/bar/baz`.

```
    'redirects' => [
       '/old-blog/*' => '/new-blog/{wildcard}', // {wilcard} will be the entire path
    ],
```

By default the package only redirects if the request has a `404` response code but it's possible to be redirected on any response code. To achieve this you may change the `redirect_status_codes` option to an array of response codes or leave it empty if you wish to be redirected no matter what the response code was sent to the URL. You may override this using the following syntax to achieve this:

```
    'redirect_status_codes' => [\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND],
```

It is also possible to optionally specify which http response code is used when performing the redirect. By default the `301 Moved Permanently` response code is set. You may override this using the following syntax:

```
    'redirects' => [
       'old-page' => ['/new-page', 302],
    ],
```

Events
------

[](#events)

The package will fire a `RouteWasHit` event when it found a redirect for the route. A `RedirectNotFound` is fired when no redirect was found.

Creating your own redirector
----------------------------

[](#creating-your-own-redirector)

By default this package will use the `Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector` which will get its redirects from the config file. If you want to use another source for your redirects (for example a database) you can create your own redirector.

A valid redirector is any class that implements the `Spatie\MissingPageRedirector\Redirector\Redirector`-interface. That interface looks like this:

```
namespace Spatie\MissingPageRedirector\Redirector;

use Symfony\Component\HttpFoundation\Request;

interface Redirector
{
    public function getRedirectsFor(Request $request): array;
}
```

The `getRedirectsFor` method should return an array in which the keys are the old URLs and the values the new URLs.

If you want to use `Route::fallback`
------------------------------------

[](#if-you-want-to-use-routefallback)

If you do not wish to overwrite the default redirector, or if you already have existing `Route::fallback` logic based on [laravel docs](https://laravel.com/docs/11.x/routing#fallback-routes), you can use this package as follow. In the bottom of your `web.php` file,

```
use Spatie\MissingPageRedirector\MissingPageRouter;
//... Your other route

Route::fallback(function (Request $request) {
    $redirectResponse = app(MissingPageRouter::class)->getRedirectFor($request);

    if ($redirectResponse !== null) {
        return $redirectResponse;
    }
    //... Your other logic
});
```

You can adjust the priority of redirect base on your needs.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

69

—

FairBetter than 100% of packages

Maintenance83

Actively maintained with recent releases

Popularity60

Solid adoption and visibility

Community37

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 56.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~114 days

Recently: every ~281 days

Total

31

Last Release

87d ago

Major Versions

0.0.1 → 1.0.02016-10-14

1.3.0 → 2.0.02017-08-31

PHP version history (4 changes)0.0.1PHP ^7.0

2.5.0PHP ^7.2

2.7.1PHP ^7.2|^8.0

2.9.4PHP ^8.0|^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (81 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (11 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (7 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (7 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (6 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (5 commits)")[![ttomdewit](https://avatars.githubusercontent.com/u/2845400?v=4)](https://github.com/ttomdewit "ttomdewit (2 commits)")[![gtapps](https://avatars.githubusercontent.com/u/16778396?v=4)](https://github.com/gtapps "gtapps (2 commits)")[![daikazu](https://avatars.githubusercontent.com/u/4039367?v=4)](https://github.com/daikazu "daikazu (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")[![chengkangzai](https://avatars.githubusercontent.com/u/43839286?v=4)](https://github.com/chengkangzai "chengkangzai (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")[![hofmannsven](https://avatars.githubusercontent.com/u/1214387?v=4)](https://github.com/hofmannsven "hofmannsven (1 commits)")[![it-can](https://avatars.githubusercontent.com/u/644288?v=4)](https://github.com/it-can "it-can (1 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (1 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (1 commits)")[![bhulsman](https://avatars.githubusercontent.com/u/612651?v=4)](https://github.com/bhulsman "bhulsman (1 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (1 commits)")[![amochohan](https://avatars.githubusercontent.com/u/2978619?v=4)](https://github.com/amochohan "amochohan (1 commits)")

---

Tags

laravelphpredirectseospatielaravel-missing-page-redirector

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/spatie-laravel-missing-page-redirector/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-missing-page-redirector/health.svg)](https://phpackages.com/packages/spatie-laravel-missing-page-redirector)
```

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
