Laravel Missing Page Redirector Package


November 4th, 2016


If you moved your site to a new domain, chances are you want the users and search engines to be directed to the correct pages on the new domain.
One way to do that is using 301 permanent redirects from old pages locations to the new pages locations. In Laravel you can define this in teh routes file like this:

1Route::get('/old-page', function () {
2 return redirect('/new-page', 301);

If you have a lot of these they can quickly fill up your routes file and if you are using dynamic pages it could get unwieldy. Spatie’s new Laravel missing page redirector package aims to make this process easier by adding your redirects to the package config file or by implementing the included Redirector interface. Let’s take a look at how this package can be set up and used.


First, install Laravel missing page redirector through Composer:

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

Add its Service provider in config/app.php:

1'providers' => [
2 ...
3 Spatie\MissingPageRedirector\MissingPageRedirectorServiceProvider::class,

Then register the Spatie\MissingPageRedirector\RedirectsMissingPages middleware in app/Http/Kernel.php:

1protected $middleware = [
2 ...
3 \Spatie\MissingPageRedirector\RedirectsMissingPages::class,
4 ],

Then, you have to publish the config file, run the following command in your terminal:

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


If you opened config/laravel-missing-page-redirector.php configuration file, you will see the following:

1return [
3 /**
4 * This is the class responsible for providing the URLs which must be redirected.
5 * The only requirement for the redirector is that it needs to implement the
6 * `Spatie\MissingPageRedirector\Redirector\Redirector`-interface
7 */
8 'redirector' => \Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector::class,
10 /**
11 * When using the `ConfigurationRedirector` you can specify the redirects in this array.
12 * You can use Laravel's route parameters here.
13 */
14 'redirects' => [
15// '/non-existing-page' => '/existing-page',
16// '/old-blog/{url}' => '/new-blog/{url}',
17 ],
  • First, you can change the source for your redirects to another source (for example a database) by creating your own redirector, the new redirector must implement Spatie\MissingPageRedirector\Redirector\Redirector interface and have getRedirectsFor method that returns an array maps the old URLs to the new URLs.
  • The second option is for specifying the redirects array for ConfigurationRedirector, just add an element for each redirect with the old URL as the key and the new URL as the value of the element.
  • You can use route parameters by wrapping them between curly braces like this '/old-blog/{url}' => '/new-blog/{url}' and you can make it optional using question mark like this: '/old-blog/{url?}' => '/new-blog/{url}',.
  • Be aware that if you want to redirect an old URL to a new URL a different domain you must add http:// or https:// before the new URL like this: '/old-page' => '

If you want an easy way to do 301 permanent redirects from old URLs to new URLs, give the Laravel missing page redirector a try.

Filed in:

Diaa Fares

Laravel Artisan, Web Developer, Tech Geek

Laravel News Partners