Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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

Original – by Freek Van der Herten – 3 minute read

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Retain your SEO worth by correctly redirecting missing pages in a Laravel application"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.