Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Retain your SEO worth by correctly redirecting missing pages in a Laravel application

When transitioning from a old site to a new one the URLs of your site 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. Our new spatie/laravel-missing-page-redirector package makes that process very easy.

When installed the easiest way to add a permanent redirect is to just add it to the config file of the package. In the configured redirects you can use (optional) route parameters like you’re used to when specifying normal routes. Here’s an example.

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,

     * 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}',
        '/old-section/{url?}' => '/new-section/{url}',

How does this work internally? Simple! If you’re application doesn’t return a 404 the package won’t to a thing. When your app does return a 404 we’ll first try to find a redirect for that request. If a redirect is found than that redirect is the result of the request.

To find the right redirect for a request and to leverage route parameters we’re instantiating a fresh router behind the scenes. The redirects are added to that router and then we’re dispatching the request on that router.

The package is also very extensible. In the config file you can specify a redirector that is responsible for providing the redirects. The default ConfigurationRedirector just reads the redirects for the config file itself. In our projects we want to let our clients specify the redirects, so we store the redirects in the database (and we’ve built some UI around that. Our DatabaseRedirector just gets its redirects from a Redirect model.

If you got tens of thousands of different redirects or if performance is a concern, you probably should manage the redirects at the webserver level. But if you’ve got only a couple of redirects using this package is definitely ok. Take a look at the package on GitHub to learn all the options. Head over to the list of Laravel packages on our company website to find out if we’ve made something that can be of use to you.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • Great package, hope it finds a use in my next project. Just to let you know though, the link is incorrect in “Take a look at the package on GitHub”.