Learning center

Capture every hop of a redirect chain

"The link takes me to the wrong page" is a report about a redirect chain, and the landing URL hides the hops. The maintainer needs every hop: each URL, status, Location header and Set-Cookie. Three tools show the chain, and one of the obvious ones undercounts it.

Three ways to see the chain, compared#

We built a fixture with a three-hop chain: localhost/r1 sent 302 to /r2, which sent 301 to 127.0.0.1/r3, a different origin, which sent 302 to /final.

ToolWhat it showed
DevTools Network panel with Preserve log onEach request as its own row, with status and the Location response header
Playwright, following request.redirectedFrom() from the final response backAll four URLs in order
performance.getEntriesByType('navigation')[0].redirectCount on the final page0, because one redirect crossed origins

A second chain with two same-origin redirects reported redirectCount as 2. Navigation Timing hides redirect details from the final page when a redirect crossed origins, so a 0 there does not mean the chain was direct. MDN: PerformanceNavigationTiming.redirectCount

Capture it in DevTools#

  1. Open the Network panel and turn on Preserve log, so the panel keeps requests across the navigation.
  2. Turn on Disable cache, or note that you left it off. A browser can serve a cached 301 without asking the server, which changes what the chain shows.
  3. Click the link or load the starting URL.
  4. For each redirect row, copy the status, the request URL, the Location header and any Set-Cookie headers.

Chrome DevTools: Network features reference

Capture it outside the browser#

curl shows each hop's headers when it follows redirects:

curl -sS -o /dev/null -D - -L 'https://example.test/promo?ref=mail'

-D - prints each response's headers, and -L follows Location. curl sends no browser cookies unless you give it a cookie jar with -b and -c, so a chain that depends on cookies will differ. That difference is evidence. Replay the request outside the page covers copying the browser's headers.

Capture it in a script#

const response = await page.goto('https://example.test/promo?ref=mail');
const chain = [];
for (let req = response.request(); req; req = req.redirectedFrom()) chain.unshift(req.url());
console.log(chain);

Add (await req.response())?.status() to each entry if the statuses matter. Playwright: request.redirectedFrom()

What to look for in each hop#

  • Dropped query parameters. A hop that redirects /promo?ref=mail to /promo/ without the query loses attribution or state.
  • Scheme and host changes. http to https, www to the apex domain and back. Two layers doing opposite rewrites make a redirect loop.
  • Cookies set on a hop. A cookie set on a redirect response is stored before the next request, if the browser accepts it. Check its Domain, Path, Secure and SameSite.
  • Permanent redirects. A 301 or 308 can be cached by the browser, so the reporter's browser may follow an old redirect the server no longer sends.

This example is fictional. Marketing reports that a newsletter link lands on the pricing page without the discount banner. The link is https://example.test/go/winter.

The Network panel with Preserve log shows three hops: /go/winter 302 to /pricing?coupon=WINTER, then 301 to https://www.example.test/pricing, then 200. The second hop, a host canonicalization rule at the CDN, rebuilt the URL without its query string. The report gave each hop's status and Location, so the maintainer went straight to the CDN rule rather than the application's link handler.

Questions#

Why does the reporter see a redirect the server no longer sends?

The browser may have cached a 301 or 308. In our fixture, a repeated navigation through a 301 loop made no requests to the server at all. Test in a fresh profile, and clear the site's cached data in the reporter's browser to confirm.

Can JavaScript or meta refresh redirects appear in the chain?

They appear as separate navigations, not as HTTP redirects. With Preserve log on you see each document request; redirectedFrom() and redirectCount do not include them.

Do I need the response bodies?

Rarely. The status and headers decide where the browser goes. A body matters only for a page that redirects with JavaScript.

Return to reproducing what someone else saw for the other layers a replay needs.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading