How to Handle Curl Infinite Redirect Loops

When using curl to fetch web resources, encountering an infinite redirect loop can halt automation scripts, waste bandwidth, and cause requests to fail. This article explains what happens when curl gets stuck in a redirect loop, how the tool inherently prevents infinite looping, and the specific command-line options you can use to mitigate and debug this behavior effectively.

What Happens During a Redirect Loop?

By default, curl does not follow HTTP redirects (status codes in the 3xx range) and will simply return the redirect response. However, if you instruct curl to follow redirects using the -L or --location flag, it will automatically make a new request to the URL specified in the response’s Location header.

If those target URLs point back to each other in a loop—for example, Page A redirects to Page B, which redirects back to Page A—curl would theoretically request these pages forever. This behavior can freeze automated scripts and spike traffic on the target servers.

Built-In Mitigation: The Redirection Limit

To prevent infinite loops from hanging your system, curl has a built-in safety mechanism.

When redirect following is enabled via -L, curl limits the number of sequential redirects it will follow. By default, this limit is set to 50. If the redirect chain exceeds 50 hops, curl aborts the operation and exits with an error:

curl: (47) Maximum redirection length exceeded

This prevents the command from running indefinitely, but waiting for 50 redirects can still cause unnecessary delay in scripts and applications.

How to Customize and Mitigate Redirect Loops

1. Reduce the Redirect Limit with --max-redirs

You can lower the maximum allowed redirects to detect loops faster. Using the --max-redirs option alongside -L allows you to define a strict threshold. For example, to stop following redirects after 5 attempts:

curl -L --max-redirs 5 https://example.com

If the server redirects more than 5 times, curl will immediately exit with error code 47, saving time and network resources.

2. Inspect the Redirect Chain

If you suspect a loop is occurring, you can diagnose the redirect path by viewing the HTTP headers. Use the -I (fetch headers only) and -L flags together to see where each redirect points:

curl -IL https://example.com

This command will print the headers for every hop in the chain, allowing you to identify which URL is redirecting back to a previous page.

3. Use Verbose Mode for Detailed Troubleshooting

For a comprehensive view of the request and response cycles, add the -v or --verbose flag. This shows the DNS lookups, TLS handshakes, and redirect behaviors in real-time:

curl -Lv https://example.com

Common Causes of Redirect Loops

If you are the administrator of the server causing the loop, the issue is typically caused by: * HTTP to HTTPS loops: The server redirects HTTP to HTTPS, but the SSL/TLS configuration on the server or reverse proxy incorrectly forwards the request back to port 80 (HTTP). * WWW vs. non-WWW conflicts: The server redirects example.com to www.example.com, while another rule simultaneously redirects www.example.com back to example.com. * Trailing slash issues: Misconfigured routing rules that constantly add or remove trailing slashes from the URL path.