A redirect repository for PSR-15 middleware packages
Middleware to redirect old urls to new urls SEO friendly.
This package is installable and autoloadable via Composer as middlewares/redirect.
composer require middlewares/redirect
Dispatcher::run([ new Middlewares\Redirect(['/old-url' => '/new-url']) ]);
You can use an array or an object extending
ArrayAccess interface with the urls to redirect, the key is the old url and the value the new.
$redirections = [ '/corporative-info' => '/about-us', '/post/2390' => '/post/new-psr15-middlewares', ]; $redirect = new Middlewares\Redirect($redirections);
Optionally, you can provide a
Psr\Http\Message\ResponseFactoryInterface as the second argument to create the redirect responses. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
$responseFactory = new MyOwnResponseFactory(); $redirect = new Middlewares\Redirect($redirections, $responseFactory);
Use temporary or permanent redirection HTTP status code for the response. Enabled by default.
//Temporary redirections (302) $redirect = (new Middlewares\Redirect($redirections))->permanent(false);
Take the query part of the URI into account when matching redirects. Enabled by default.
//Ignore url query $redirect = (new Middlewares\Redirect($redirections))->query(false);
This option accepts an array with the allowed HTTP request methods. (By default is:
//Redirects GET and HEAD requests $redirect = (new Middlewares\Redirect($redirections))->method(['GET', 'HEAD']);
The MIT License (MIT). Please see LICENSE for more information.