PHPackages                             spatie/guzzle-redirect-history-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. spatie/guzzle-redirect-history-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

spatie/guzzle-redirect-history-middleware
=========================================

A Guzzle middleware to keep track of redirects

1.0.1(4y ago)1914.8k↑69.4%41MITPHPPHP ^8.0CI passing

Since Oct 6Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/spatie/guzzle-redirect-history-middleware)[ Packagist](https://packagist.org/packages/spatie/guzzle-redirect-history-middleware)[ Docs](https://github.com/spatie/guzzle-redirect-history-middleware)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-guzzle-redirect-history-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (5)Versions (4)Used By (1)

A Guzzle middleware to keep track of redirects
==============================================

[](#a-guzzle-middleware-to-keep-track-of-redirects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2a65687ddd2774b0f1eb7be7be0da7dec7c44d7cabf4990dacd86eb6b4748f8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f67757a7a6c652d72656469726563742d686973746f72792d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/guzzle-redirect-history-middleware)[![Tests](https://github.com/spatie/guzzle-redirect-history-middleware/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/guzzle-redirect-history-middleware/actions/workflows/run-tests.yml)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/3a827b2300cfe5b2964366c06692a03874419d8fdb809bff65f21843ab7cb39a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f67757a7a6c652d72656469726563742d686973746f72792d6d6964646c65776172652f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/spatie/guzzle-redirect-history-middleware/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/64d31fef56dbbbf0ae44f405dc96913091ac3fa79226c6becf14f9190e310c81/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f67757a7a6c652d72656469726563742d686973746f72792d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/guzzle-redirect-history-middleware)

This package contains middleware for [Guzzle](https://docs.guzzlephp.org/en/stable/) that allows you to track redirects that happened during a request.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/0868423d2450c2de6f4ca82fc4921497896913dca5856be9428081bc18ea70bb/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f67757a7a6c652d72656469726563742d686973746f72792d6d6964646c65776172652e6a70673f743d31)](https://spatie.be/github-ad-click/guzzle-redirect-history-middleware)

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
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/guzzle-redirect-history-middleware
```

Usage
-----

[](#usage)

Here is a quick example of how you can use the `Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware` to store the redirect history in a `Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistory` instance.

```
use Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistory;
use Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware;

/*
 * First create a new instance of `RedirectHistory`
 * This instance can be used after the requests to get the redirects.
 */
$redirectHistory = new RedirectHistory();

/*
 * This is the default way to add a middleware to Guzzle
 * default middleware stack.
 */
$stack = HandlerStack::create();
$stack->push(RedirectHistoryMiddleware::make($redirectHistory));

/*
 * Let's create Guzzle client that uses the middleware stack
 * containing our `RedirectHistoryMiddleware`.
 */
$client = new Client([
    'handler' => $stack,
]);

/*
 * Now, let's make a request.
 */
$response = $client->get($anyUrl);

/*
 * And tada, here are all the redirects performed
 * during the request.
 */
$redirects = $redirectHistory->toArray();
```

`$redirects` is an array of which item is an array with these keys:

- `status`: the status code of the response
- `url`: the URL of the performed request that resulted in a redirect

So if you make a request to `https://example.com/page-a` which redirects to `/page-b` which finally redirects to `/page-c`, this will be the content of `$redirects`

```
[
    ['status' => 302, 'url' => 'https://example.com/page-a'],
    ['status' => 302, 'url' => 'https://example.com/page-b'],
    ['status' => 200, 'url' => 'https://example.com/page-c'],
];
```

Even if your initial request results in a `\GuzzleHttp\Exception\TooManyRedirectsException`, the `RedirectHistory` will still contain the performed redirects

Why we created this package
---------------------------

[](#why-we-created-this-package)

Guzzle has [built-in support for tracking redirects](https://docs.guzzlephp.org/en/stable/request-options.html#allow-redirects). Unfortunately, it isn't that developer friendly to use. You'll have to manipulate and combine arrays found in the `X-Guzzle-Redirect-History` and `X-Guzzle-Redirect-Status-History` headers.

Also, when hitting an exception such as `TooManyRedirectsException`, these headers won't be filled.

Our package makes it easy to retrieve the redirects in a sane format. You're also able to get the redirect history even if the request ultimately fails.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Contributing
------------

[](#contributing)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

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

License
-------

[](#license)

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

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance54

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

3

Last Release

1676d ago

Major Versions

0.0.1 → 1.0.02021-10-06

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (18 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (5 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![bessone](https://avatars.githubusercontent.com/u/1089510?v=4)](https://github.com/bessone "bessone (1 commits)")

---

Tags

guzzlehttpmiddlewarephpredirectspatieguzzle-redirect-history-middleware

###  Code Quality

TestsPest

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/spatie-guzzle-redirect-history-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-guzzle-redirect-history-middleware/health.svg)](https://phpackages.com/packages/spatie-guzzle-redirect-history-middleware)
```

###  Alternatives

[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[spatie/laravel-webhook-client

Receive webhooks in Laravel apps

1.2k11.7M65](/packages/spatie-laravel-webhook-client)[spatie/guzzle-rate-limiter-middleware

A rate limiter for Guzzle

1675.0M29](/packages/spatie-guzzle-rate-limiter-middleware)[muhammadhuzaifa/telescope-guzzle-watcher

Telescope Guzzle Watcher provide a custom watcher for intercepting http requests made via guzzlehttp/guzzle php library. The package uses the on\_stats request option for extracting the request/response data. The watcher intercept and log the request into the Laravel Telescope HTTP Client Watcher.

98239.8k1](/packages/muhammadhuzaifa-telescope-guzzle-watcher)[ory/hydra-client-php

Documentation for all of Ory Hydra's APIs.

1710.8k](/packages/ory-hydra-client-php)[meteocontrol/vcom-api-client

HTTP Client for meteocontrol's VCOM API - The VCOM API enables you to directly access your data on the meteocontrol platform.

175.7k1](/packages/meteocontrol-vcom-api-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
