Skip to content

This is a small piece of PSR-15 (HTTP Server Request Handlers) middleware that redirects a request from one domain to a new one; such as an old one that's being deprecated to a new one when a site is being re-hosted

License

Notifications You must be signed in to change notification settings

settermjd/redirect-to-new-domain-middleware

Redirect to new domain middleware

This is a small piece of PSR-15 (HTTP Server Request Handlers) middleware that redirects a request from one domain to a new one; such as an old one that's being deprecated to a new one when a site is being re-hosted.

Prerequisites

To use the package, you'll need the following:

  • PHP 8.3

Getting Started

With Mezzio

To use the project, in a Mezzio application, you first need to provide it with the configuration that it needs. If you use RedirectToNewDomainMiddlewareFactory to instantiate it, which is the default option, it will look in the $_ENV superglobal first, then in the application's configuration array.

A sample configuration is available in config/sample.redirecttonewdomainmiddleware.global.php. Copy that to config/autoload, renaming it to redirecttonewdomainmiddleware.global.php. Then, update the values for the old and new keys to the required values for your application. These values are:

  • old: The domain that is being deprecated or is to be redirected from
  • new: The domain, which requests are to be redirected to

Then, for all routes that need the functionality, load the middleware as early in the route's pipeline as possible. This will ensure that the redirect happens before any other functionality is invoked. For example, in a ConfigProvider class, you could define a route as follows:

[
    'path'            => '/',
    'middleware'      => [
        Settermjd\Middleware\RedirectToNewDomainMiddleware::class,
        App\Handler\HomePageHandler::class,
    ]
    'allowed_methods' => ['GET'],
],

Alternatively, add the middleware to the global pipeline in config/pipeline.php as early as possible, such as in the following example:

$app->pipe(Settermjd\Middleware\RedirectToNewDomainMiddleware::class);

You should not need to do anything else, as RedirectToNewDomainMiddleware will be registered with the application's DI container via the package's ConfigProvider class, which is automatically loaded when the package is installed into a Mezzio application.

Contributing

If you want to contribute to the project, whether you have found issues with it or just want to improve it, here's how:

  • Issues: ask questions and submit your feature requests, bug reports, etc
  • Pull requests: send your improvements

Did You Find The Project Useful?

If the project was useful and you want to say thank you and/or support its active development, here's how:

  • Add a GitHub Star to the project
  • Write an interesting article about the project wherever you blog

Disclaimer

No warranty expressed or implied. Software is as is.

About

This is a small piece of PSR-15 (HTTP Server Request Handlers) middleware that redirects a request from one domain to a new one; such as an old one that's being deprecated to a new one when a site is being re-hosted

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages