This package provides an easy way to load redirects from the database instead of defining them in your route files. By default the package will only load your redirects when the original request results in a 404.
You can install the package via composer:
composer require esign/laravel-redirects
The package will automatically register a service provider.
For the redirects to be active you must register the Esign\Redirects\Http\Middleware\CheckForRedirects
middleware.
// app/Http/Kernel.php
protected $middleware = [
...
Esign\Redirects\Http\Middleware\CheckForRedirects::class,
];
This package comes with a migration to store your redirects. In case you want to modify this migration you may publish it using:
php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="migrations"
Next up, you can publish the configuration file:
php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="config"
The config file will be published as config/redirects.php
with the following content:
return [
/**
* This is the model used by the DatabaseRedirector.
* It should implement the RedirectContract interface and extend the Model class.
*/
'redirect_model' => Esign\Redirects\Models\Redirect::class,
/**
* This class provides the redicect url's to the CheckForRedirects middleware.
* It should implement the RedirectorContract interface.
*/
'redirector' => Esign\Redirects\Redirectors\DatabaseRedirector::class,
/**
* The key that will be used to cache the redirects.
*/
'cache_key' => 'redirects',
/**
* The amount of seconds the redirects will be cached for.
*/
'cache_remember' => 15,
];
Defining redirects in the database is pretty straight forward:
Redirect::create([
'old_url' => 'my-old-url',
'new_url' => 'my-new-url',
]);
It's also possible to define route parameters just like the way you're used to in Laravel:
Redirect::create([
'old_url' => 'my-old-url/{slug}',
'new_url' => 'my-new-url/{slug}',
]);
When using route parameters, the following parameters are reserved by Laravel and cannot be used: destination
and status
.
You may even swap the order of the route parameters
Redirect::create([
'old_url' => 'my-old-url/{slug}/{year}',
'new_url' => 'my-new-url/{year}/{slug}',
]);
By default a 302
status will be used, but you can also supply a custom status code
Redirect::create([
'old_url' => 'my-old-url/{slug}/{year}',
'new_url' => 'my-new-url/{year}/{slug}',
'status_code' => 301,
]);
It's also possible to redirect to external urls
Redirect::create([
'old_url' => 'my-old-url',
'new_url' => 'https://www.esign.eu',
]);
This package also allows you to define constraints for your routes:
Redirect::create([
'old_url' => 'user/{id}',
'new_url' => 'users/{id}',
'constraints' => ['id' => '[0-9]+'],
]);
Redirect::create([
'old_url' => 'nl/{any?}',
'new_url' => 'nl-be/{any?}',
'constraints' => ['any' => '.*'],
]);
This package also ships with a DatabaseWildcardRedirector
, which allows you to define redirects by using *
as a wildcard. This will automatically apply a constraint to match any trailing url segments:
Redirect::create([
'old_url' => 'my-old-url/*',
'new_url' => 'my-new-url/*',
]);
composer test
The MIT License (MIT). Please see License File for more information.