Why cURL Doesn't Follow Redirects by Default (and How to Fix It)
ArticleUnderstand how cURL handles HTTP redirects, why it doesn’t follow them automatically, and how to control redirect behavior using -L and related flags.
When you make an HTTP request with cURL, the tool talks directly to a server and returns the response. But unlike web browsers, cURL does not automatically follow redirects. That means it does not automatically make a second request when the server says a resource has moved using an HTTP 3xx status code (such as 301, 302, or 303).
This behavior can be surprising if you expect cURL to behave like Chrome or Firefox, which follow redirects silently behind the scenes. However, because cURL is designed as a predictable command-line HTTP client, it waits for explicit instructions before following redirect chains.
Understanding how to enable and control redirect following is important when debugging web services, downloading from URLs that redirect, or working with APIs that issue temporary or permanent redirects.
Why cURL Doesn’t Follow Redirects by Default
Servers send redirects using HTTP responses in the 300–399 range. These responses include a Location header telling the client where to go next. For example:
HTTP/1.1 301 Moved Permanently
Location: [https://example.com/new-location](https://example.com/new-location)
By default, cURL displays this response without going to the new Location. This gives developers full visibility into the redirect itself and allows them to handle it manually, which is especially useful in scripts, automation, and debugging scenarios.
Enabling Redirect Following: -L / --location
To make cURL follow redirects automatically, use the -L option (short for --location). This tells cURL to follow the redirect using the URL provided in the Location header.
curl -L https://example.com/redirect
In this command:
- cURL sends a request to the initial URL.
- If the server responds with a redirect, cURL follows the new URL.
- The final response (after all redirects) is printed to the terminal.
You can also save the final response to a file:
curl -L -o page.html https://example.com/redirect
Defaults and Limits
When using -L, cURL follows up to 50 redirects by default. This safeguard prevents infinite redirect loops caused by misconfigured servers.
To limit redirects manually, use --max-redirs:
curl -L --max-redirs 10 https://example.com/redirect
How Redirects Work with Different HTTP Methods
By default, when cURL follows redirects for status codes like 301, 302, or 303, it changes POST requests into GET requests. This mirrors browser behavior but may not always be desirable.
To preserve the original HTTP method, you can use:
--post301--post302--post303
Use these options carefully, depending on how your server expects redirected requests to behave.
Debugging and Viewing Redirect Chains
To inspect every step of a redirect chain, combine -L with verbose output:
curl -L -v https://example.com/redirect
This shows headers, intermediate URLs, and responses for each redirect, making it easier to diagnose unexpected behavior.
Redirects in Web Scraping and Automation
Redirect handling becomes especially important in web scraping and data collection workflows, where URLs frequently change or route traffic through tracking and validation layers.
In large-scale scraping systems, improper handling of redirects can lead to broken crawls, missing data, or incorrect endpoint resolution. This is why many scraping solutions—such as those discussed in Mrscraper’s guide to web scraping fundamentals—emphasize understanding HTTP behavior, status codes, and redirect logic before building production scrapers.
Handling Credentials and Security
When using -L, cURL protects your credentials. If a redirect points to a different host, cURL will not forward authentication headers by default.
If you fully trust the redirect chain, you can use:
--location-trusted
⚠️ Warning: Only use this option when you are certain all redirect destinations are under your control.
Pro Tip: Extracting Only the Final URL
If you only want the final destination URL (not the content), use:
curl -Ls -o /dev/null -w "%{url_effective}\n" https://bit.ly/example-link
This follows all redirects silently and prints only the final resolved URL—perfect for link auditing or cleanup tasks.
Why cURL Can’t Follow Client-Side Redirects
cURL only handles server-side HTTP redirects. It cannot follow:
- JavaScript redirects (
window.location) - HTML meta refresh tags
These require a real browser or a headless browser that executes JavaScript.
Common Use Cases
Following redirects with cURL is useful for:
- Downloading files from redirected URLs
- Testing API endpoints with redirect logic
- Debugging URL migrations
- Automating data collection pipelines
Conclusion
Using -L allows cURL to follow redirects automatically while still giving developers full control over request behavior, limits, and security. Understanding how redirects work is essential when interacting with real-world APIs, web services, and scraping targets.
If cURL doesn’t return the content you expect, always check for redirects first—and try adding -L to see the final destination.
Find more insights here
What Is an API Call — A Deep Dive Into How Applications Communicate
Learn what an API call is, how it works, common HTTP methods, real-world examples, and why API calls...
What Is Parsing? A Clear Definition and Practical Understanding
Learn what parsing means in computer science and language, how it works, common parser types, and re...
Python glob: How to Use Pattern Matching for File and Directory Search
Learn how to use Python’s glob module for file and directory searches using wildcard patterns, recur...