Sub topic 5
Redirect chains and loops
This chapter assumes you can read HTTP status codes and know the difference between 301 and 302 redirects. It also assumes you can run curl from a terminal.
What a chain is
A redirect chain happens when a URL redirects to another URL that also redirects. A request for /old-page might go to /older-page-v2, then to /new-page, and only then return 200. Each step is a hop, and each hop costs a full request and response.
Chains usually build up over time. A page is renamed in 2021, renamed again in 2023, and nobody updates the first redirect. Other common sources are HTTP to HTTPS, www to non-www, and trailing-slash rules that stack on top of a page redirect.
Why chains and loops hurt
- Users wait for every extra round trip before the page starts to load.
- Googlebot follows a limited number of hops (Google documents up to 10) and then gives up. Other crawlers may stop sooner.
- Link signals can be diluted or delayed along a long path, so keep the path short.
A redirect loop is worse. URL A redirects to B and B redirects back to A, so the request never reaches a final page. Browsers stop with an error such as ERR_TOO_MANY_REDIRECTS. Crawlers record the URL as an error.
Seeing the chain with curl
The -I flag sends a HEAD request, and -L follows redirects. The grep keeps only the status lines and Location headers:
curl -sIL --max-redirs 10 https://example.com/old-page | grep -iE '^(HTTP|location)'A chain looks like this:
HTTP/2 301
location: /older-page-v2
HTTP/2 301
location: /new-page
HTTP/2 200Count the HTTP lines. More than two means at least one redirect hop. For a quick summary, use curl's write-out variables:
curl -sL --max-redirs 10 -o /dev/null -w '%{num_redirects} redirects, final status %{http_code}, final URL %{url_effective}\n' https://example.com/old-pageA loop ends with curl: (47) Maximum (10) redirects followed. Without --max-redirs, curl follows up to 50 hops before it stops.
Fixing chains and loops
- Find the final destination, the first URL that returns 200.
- Point every old URL straight at that destination with a single 301 or 308.
- Update internal links so they use the final URL. This removes the redirect from normal traffic entirely.
- For a loop, find the two rules that contradict each other. Typical causes are a CMS rule fighting a server rule, or a slash-adding rule fighting a slash-removing rule. Remove one of them.
Exercise
curl reports "3 redirects, final status 200" for /a. The hops are /a to /b, /b to /c, and /c to /d. What should the redirect rules be after you fix the chain?
Show answer
Change them so /a, /b, and /c each redirect directly to /d with a single permanent redirect (301 or 308). Also update internal links to point at /d, so normal visitors never hit a redirect.