middlewares / redirect
A redirect repository for PSR-15 middleware packages
Installs: 11 517
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0 || ^4.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to redirect old urls to new urls SEO friendly.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/redirect.
composer require middlewares/redirect
Example
Dispatcher::run([ new Middlewares\Redirect(['/old-url' => '/new-url']) ]);
Usage
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);
permanent
Use temporary or permanent redirection HTTP status code for the response. Enabled by default.
//Temporary redirections (302) $redirect = (new Middlewares\Redirect($redirections))->permanent(false);
query
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);
method
This option accepts an array with the allowed HTTP request methods. (By default is: ['GET']
.)
//Redirects GET and HEAD requests $redirect = (new Middlewares\Redirect($redirections))->method(['GET', 'HEAD']);
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.