Errors and troubleshooting
How do I follow redirects in cURL without loops?
Add -L to follow redirects, --max-redirs to cap them, and a cookie jar (-c and -b) so cookies set along the way are sent back. Missing cookies are the usual reason a redirect chain turns into a loop.
The answer
By default cURL does not follow redirects; it prints the 301, 302, 303, 307 or 308 response and stops. -L (--location) makes it follow the Location header, up to a default cap that depends on your cURL version. Loops happen when the site redirects you somewhere that redirects back, usually because it set a cookie on the first hop (a session, a consent flag, a region choice) and expects it on the next request. cURL only keeps cookies if you give it a cookie jar. So the working recipe is:
curl -sSL --max-redirs 10 \
-c ./jar.txt -b ./jar.txt \
-A "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36" \
-o page.html -w "final=%{url_effective} hops=%{num_redirects} code=%{http_code}\n" \
"https://store.example.com/go/summer-sale"
-c writes cookies received, -b reads them back; pointing both at the same file keeps state across hops and across runs. -w prints where you ended up and how many hops it took, which is the fastest way to see a loop or an unexpected destination.
Tracing the chain
When a loop or wrong destination appears, look at every hop:
curl -sSL --max-redirs 8 -c jar.txt -b jar.txt -D - -o /dev/null \
"https://store.example.com/go/summer-sale" | grep -Ei "^(HTTP/|location:|set-cookie:)"
-D - dumps response headers for each hop. Read them in order: if you see the same two locations alternating, with a Set-Cookie on one of them, cookies are the issue. If the chain bounces between http and https or between www and the bare domain, the site has conflicting rules, often triggered by a header you send (or do not send).
The usual causes of loops
- No cookie jar. The site sets a session or consent cookie and redirects to check it. Without
-b, the check fails and it redirects again. - Bot checks. Some protections redirect to a challenge, then back. Without passing the challenge, you circle. A Cloudflare or AWS WAF challenge needs a browser or a clearance cookie; cURL alone will not execute its JavaScript.
- Language or region detection. Redirects based on
Accept-Languageor IP can conflict with a region cookie. Send a consistent language header. - Protocol confusion. A proxy or load balancer that does not forward the original scheme causes http/https ping-pong on the server side. Nothing you can fix except trying the other scheme directly.
POST requests and redirects
Following a 301, 302 or 303 after a POST, cURL switches to GET for the next request, as browsers do. 307 and 308 keep the method and body. If a form post must stay a POST across a 301 or 302, add --post301 or --post302. Do not use -X POST together with -L: it forces POST on every hop, which breaks the normal switch to GET and can itself produce loops or errors.
Credentials across hosts
When a redirect leads to another host, cURL does not send credentials given with -u to the new host unless you add --location-trusted. That protects you from leaking passwords to third parties; only use the flag when you trust every hop.
When the loop is a challenge
If the trace shows a hop to a page containing a Turnstile widget, a /cdn-cgi/challenge-platform/ path or an AWS WAF CAPTCHA script, the loop is a bot check. Solve it, then repeat the request with the resulting cookie in your jar and the same user agent. For Cloudflare's interstitial, a Cloudflare Challenge task returns cf_clearance plus the user agent to use; for AWS WAF, an AWS WAF task returns the aws-waf-token cookie value. Add the cookie with -b "cf_clearance=..." alongside your jar. The guide on solving CAPTCHAs with cURL shows the API calls from the shell.
Quick checklist
- Use
-Lwith--max-redirsso loops fail fast. - Always use a cookie jar for sites with sessions.
- Send a realistic user agent and
Accept-Language. - Inspect hops with
-D -and-w '%{url_effective}'.
Common questions
Why does cURL not follow redirects by default?
It shows exactly what the server returned unless you ask it to follow with -L, which keeps behavior explicit.
What does "Maximum redirects followed" mean?
cURL hit the --max-redirs limit, which usually means a loop. Trace the hops and add a cookie jar.
How do I see where a redirect chain ends?
Add -w with %{url_effective} and %{num_redirects} to print the final URL and the hop count.
Why does my POST turn into a GET after a redirect?
cURL follows browser behavior for 301, 302 and 303. Use --post301 or --post302 to keep POST, or expect 307 and 308.
Can cURL pass a Cloudflare challenge redirect?
Not by itself, since it cannot run the challenge script. Obtain a clearance cookie and send it with the same user agent.
Keep reading
- HTTP redirectResponses that send the client to another URL, the codes that do it, and the redirects that lead to challenges.
- CookieSmall values a site stores in the client, which carry sessions and the clearance proof issued after a passed challenge.
- Solve any CAPTCHA with cURLA portable Bash script on curl and jq: send any task JSON file, poll at the right pace, print the solution.
- What does HTTP 403 mean when scraping?A 403 means the server understood the request and refused it, usually because of IP, headers, missing cookies or a challenge.