Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/spatie/laravel-missing-page-redirector

Redirect missing pages in your Laravel application
https://github.com/spatie/laravel-missing-page-redirector

laravel php redirect seo

Last synced: 2 days ago
JSON representation

Redirect missing pages in your Laravel application

Awesome Lists containing this project

README

        

# Redirect missing pages in your Laravel application

[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-missing-page-redirector.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-missing-page-redirector)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![run-tests](https://github.com/spatie/laravel-missing-page-redirector/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-missing-page-redirector/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-missing-page-redirector.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-missing-page-redirector)

When transitioning from a old site to a new one your URLs may change. If your old site was popular you probably want to retain your SEO worth. One way of doing this is by providing [permanent redirects from your old URLs to your new URLs](https://support.google.com/webmasters/answer/93633?hl=en). This package makes that process very easy.

When installed you only need to [add your redirects to the config file](https://github.com/spatie/laravel-missing-page-redirector#usage). Want to use the database as your source of redirects? [No problem](https://github.com/spatie/laravel-missing-page-redirector#creating-your-own-redirector)!

## Support us

[](https://spatie.be/github-ad-click/laravel-missing-page-redirector)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

## Installation

You can install the package via composer:

``` bash
composer require spatie/laravel-missing-page-redirector
```

The package will automatically register itself.

Next, prepend/append the `Spatie\MissingPageRedirector\RedirectsMissingPages` middleware to your global middleware stack:

```php
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->append([
\Spatie\MissingPageRedirector\RedirectsMissingPages::class,
]);
})
```

Finally you must publish the config file:

```php
php artisan vendor:publish --provider="Spatie\MissingPageRedirector\MissingPageRedirectorServiceProvider"
```

This is the contents of the published config file:

```php
return [
/*
* This is the class responsible for providing the URLs which must be redirected.
* The only requirement for the redirector is that it needs to implement the
* `Spatie\MissingPageRedirector\Redirector\Redirector`-interface
*/
'redirector' => \Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector::class,

/*
* By default the package will only redirect 404s. If you want to redirect on other
* response codes, just add them to the array. Leave the array empty to redirect
* always no matter what the response code.
*/
'redirect_status_codes' => [
\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND
],

/*
* When using the `ConfigurationRedirector` you can specify the redirects in this array.
* You can use Laravel's route parameters here.
*/
'redirects' => [
// '/non-existing-page' => '/existing-page',
// '/old-blog/{url}' => '/new-blog/{url}',
],

];
```

## Usage

Creating a redirect is easy. You just have to add an entry to the `redirects` key in the config file.

```php
'redirects' => [
'/non-existing-page' => '/existing-page',
],
```

You may use route parameters like you're used to when using Laravel's routes:

```php
'redirects' => [
'/old-blog/{url}' => '/new-blog/{url}',
],
```

Optional parameters are also... an option:

```php
'redirects' => [
'/old-blog/{url?}' => '/new-blog/{url}',
],
```

Finally, you can use an asterix (`*`) as a wildcard parameter that will match multiple URL segments (see [encoded URL slashes in the Laravel docs](https://laravel.com/docs/master/routing#parameters-encoded-forward-slashes) for more info). This is useful when you want to redirect a URL like `/old-blog/foo/bar/baz` to `/new-blog/foo/bar/baz`.

```php
'redirects' => [
'/old-blog/*' => '/new-blog/{wildcard}', // {wilcard} will be the entire path
],
```

By default the package only redirects if the request has a `404` response code but it's possible to be redirected on any response code.
To achieve this you may change the ```redirect_status_codes``` option to an array of response codes or leave it empty if you wish to be redirected no matter what the response code was sent to the URL.
You may override this using the following syntax to achieve this:

```php
'redirect_status_codes' => [\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND],
```

It is also possible to optionally specify which http response code is used when performing the redirect. By default the ```301 Moved Permanently``` response code is set. You may override this using the following syntax:

```php
'redirects' => [
'old-page' => ['/new-page', 302],
],
```

## Events

The package will fire a `RouteWasHit` event when it found a redirect for the route. A `RedirectNotFound` is fired when no redirect was found.

## Creating your own redirector

By default this package will use the `Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector` which will get its redirects from the config file. If you want to use another source for your redirects (for example a database) you can create your own redirector.

A valid redirector is any class that implements the `Spatie\MissingPageRedirector\Redirector\Redirector`-interface. That interface looks like this:

```php
namespace Spatie\MissingPageRedirector\Redirector;

use Symfony\Component\HttpFoundation\Request;

interface Redirector
{
public function getRedirectsFor(Request $request): array;
}

```

The `getRedirectsFor` method should return an array in which the keys are the old URLs and the values the new URLs.

## If you want to use `Route::fallback`

If you do not wish to overwrite the default redirector, or if you already have existing `Route::fallback` logic based on [laravel docs](https://laravel.com/docs/11.x/routing#fallback-routes), you can use this package as follow.
In the bottom of your `web.php` file,

```php
use Spatie\MissingPageRedirector\MissingPageRouter;
//... Your other route

Route::fallback(function (Request $request) {
$redirectResponse = app(MissingPageRouter::class)->getRedirectFor($request);

if ($redirectResponse !== null) {
return $redirectResponse;
}
//... Your other logic
});
```
You can adjust the priority of redirect base on your needs.

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Testing

``` bash
$ composer test
```

## Contributing

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

## Security

If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.

## Credits

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.