Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlexanderWright's avatar

The best way to redirect old pages.

I'm working on an event management system, where events have a public route of /event/{event_id}.

When some older events have passed, I need to redirect links to next year's event.

A previous developer has listed redirects in the routes.php file, but this obviously requires frequent modifications of the code.

I think the correct way to handle this is in a middleware that detects the old event, and redirects to the new, but I'm at a loss as to where to store the data. The obvious answer would be to store in the db, but wouldn't that result in an extra db access every request?

Suggestions welcomed, please!

0 likes
4 replies
LaryAI's avatar
Level 58

One possible solution to handle redirects for old event pages is to use a database table to store the redirection mappings. This way, you can easily manage the redirects without modifying the code.

Here's a step-by-step solution:

  1. Create a new table in your database to store the redirection mappings. The table can have columns like old_url and new_url.

  2. In your middleware, retrieve the requested URL and check if it matches any entry in the redirection table.

  3. If a match is found, redirect the user to the corresponding new URL using the redirect() function provided by Laravel.

  4. If no match is found, continue with the normal request flow.

Here's an example implementation of the middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;

class RedirectOldEvents
{
    public function handle($request, Closure $next)
    {
        $oldUrl = $request->path();

        $redirect = DB::table('redirections')
            ->where('old_url', $oldUrl)
            ->first();

        if ($redirect) {
            return redirect($redirect->new_url);
        }

        return $next($request);
    }
}

Make sure to register the middleware in your app/Http/Kernel.php file under the $middleware array.

protected $middleware = [
    // ...
    \App\Http\Middleware\RedirectOldEvents::class,
];

Remember to replace 'redirections' with the actual name of your redirection table.

With this solution, you can easily manage the redirection mappings in the database without modifying the code. However, keep in mind that there will be an extra database query for each request. If the number of redirection mappings is relatively small, the performance impact should be negligible.

1 like
rodrigo.pedra's avatar

Well, if you need to store the data, you will need to access some kind of storage anyhow.

By typing manually on the routes files, the storage accessed is the routes file.

Which impacts every request, not only redirects, as a huge routes file has to be parsed.

If you want to avoid a call to a database server which is not running locally, take a look at the Sushi package by Caleb Porzio. He is Livewire's creator.

It allows you to make an Eloquent Model work with a SQLite database, which is a local file, and works fine for simultaneous accesses, as long as you don't have too many simultaneous writes.

https://github.com/calebporzio/sushi

The logic will be similar to the one proposed by @laryai , but you will be reading from a SQLite file instead of reaching to your database server.

1 like
martinbean's avatar

@alexanderwright Can you not instead display a big notice on the event page, and a link to the next occurrence?

I’d be hesitant to just blindly redirect to the “next” event as they’re technically not the same resource. Event A != Event B.

Snapey's avatar

Presumably you are deleting the event?

When a request like /event/{event_id} comes in, I would tackle it as;

  • does the event_id exist - no, return 404
  • yes, but its in the past
    • if it has reference to a next event, redirect to that
    • show details of the old event and a notice that it has passed

and not, delete the past events.

When adding a new event, find the previous occurrence of the same event and update a next_event_id property on the previous version

Please or to participate in this conversation.