Guides & Tutorials

How to Follow Redirects with Curl

A hands-on guide to following redirects with curl, covering the -L flag, redirect limits, header inspection, method handling and using redirects through a proxy.

By default, curl does something surprising to newcomers: when a server returns a redirect, curl shows you the redirect response rather than following it to the final destination. That is fine for inspection, but if you actually want the content at the end of the chain, you need to tell curl to follow redirects explicitly.

This guide explains how redirects work, how to make curl follow them safely, and how to inspect, limit and debug redirect chains. We will also cover what happens to request methods during redirects and how this all behaves when you route requests through a proxy.

Quick answer

Adding -L makes curl follow redirects, but production-grade redirect handling means controlling which protocols are allowed, deciding whether auth headers survive cross-host hops, and capturing the full timing and status of each step. The redirect chain is also where security and correctness bugs hide: open redirects, HTTP-to-HTTPS downgrades, and silent method changes. Treat -L as the start, then add the guardrails that keep automated jobs safe and observable.

Key takeaways

  • <code>--proto-redir</code> lets you restrict which protocols curl will follow to, blocking unwanted downgrades
  • <code>--location-trusted</code> resends auth credentials to a redirected host, which is powerful and risky
  • <code>%{num_redirects}</code> and <code>%{time_redirect}</code> in <code>-w</code> expose how many hops and how long they took
  • A relative <code>Location</code> header resolves against the current URL, so the final host can differ from what you typed
  • Shortener and tracking links often chain several hops, each a chance to leak the referrer or change protocol
  • Through a proxy, every hop inherits the same exit IP, so geo-behaviour stays consistent across the chain

Why curl does not follow redirects by default

When a web server wants to send you elsewhere, it responds with a 3xx status code such as 301 (moved permanently) or 302 (found), along with a Location header pointing to the new URL. By default curl prints that response and stops, because it does not assume you want to chase the chain. This default is deliberate: it lets you see exactly what a server returned without hidden hops.

The -L flag: following redirects

To make curl follow redirects to the final URL, add the -L (or --location) flag:

curl -L https://example.com

With -L, curl reads the Location header on each 3xx response and automatically requests the next URL until it reaches a non-redirect response or hits the redirect limit. This is what most people actually want when they say "fetch this page".

Seeing the redirect chain

To understand where a URL leads, combine following with verbose or header output. Showing response headers reveals each hop:

curl -IL https://example.com

Here -I fetches headers only and -L follows the chain, so you see the status code and Location for every step. For deeper debugging, the verbose flag exposes the full request and response detail:

curl -vL https://example.com

Reading the chain this way helps you spot redirect loops, unexpected destinations, or a mix of secure and insecure hops.

Limiting how many redirects curl follows

Uncontrolled redirects can loop forever or lead somewhere you did not intend. Use --max-redirs to cap the number of hops:

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

If the chain exceeds the limit, curl stops and reports an error rather than continuing indefinitely. Setting a sensible cap is good practice in scripts and automated jobs where you cannot watch the output manually.

How request methods behave during redirects

Redirects can change which HTTP method curl uses. For some status codes, a POST request may be turned into a GET on the next hop, which can quietly break an API call. If you need the method and body preserved across redirects, the relevant options are:

  • --post301 — keep using POST after a 301 response.
  • --post302 — keep using POST after a 302 response.
  • --post303 — keep using POST after a 303 response.

When you are debugging an integration that "works in the browser but fails in curl", a silent method change during a redirect is a common culprit worth checking.

Following redirects through a proxy

Routing curl through a proxy works seamlessly with redirect following. You point curl at the proxy and add -L as usual:

curl -L -x http://user:pass@proxy-host:port https://example.com

Each hop in the redirect chain then travels through the proxy, which is exactly what you want when testing how a site responds from a particular IP or region. Because every redirected request goes through the same proxy, you see the destination as that IP would, which is useful for geo-testing and verifying location-based behaviour.

For this kind of testing and for larger automated jobs, the choice of proxy provider affects reliability and cost. Cheapest Proxies (https://cheapest-proxies.com/) is our featured value pick and a strong value-focused option worth considering when you want to run redirect checks or scraping at scale without overspending on capacity you do not need.

Common redirect pitfalls and tips

  • Stripped headers — by default some sensitive headers may not be resent to a different host; review curl's options if your auth header is being dropped on a cross-host redirect.
  • Insecure hops — watch for chains that bounce through plain HTTP before returning to HTTPS, which can expose data.
  • Cookies — if a site relies on cookies set mid-chain, add a cookie jar so they persist across hops.
  • Final URL — use -w "%{url_effective}" to print the URL curl actually ended on.

Comparison snapshot

A quick value-first shortlist — Cheapest Proxies leads as the featured pick. Qualitative labels only; confirm exact plans before buying.

ProviderBest forProfileValue
Bright DataEnterprises needing huge pools and compliance controlsEnterprise FocusedPremium
OxylabsLarge-scale scraping and data APIsEnterprise FocusedPremium
Smartproxy (Decodo)Newcomers who want an easy dashboardBeginner FriendlyGood
SOAXPrecise city and carrier targetingAutomation FriendlyGood

Locking down which protocols a redirect may use

The base guide covers -L and --max-redirs, but the more subtle control is which protocols curl is willing to follow into. By default a chain could redirect from HTTPS to HTTP, or even to a non-web protocol, depending on the Location headers. The --proto-redir option constrains this explicitly, for example restricting follows to HTTPS only. In automated jobs that handle anything sensitive, refusing to be downgraded to plain HTTP mid-chain is a meaningful safeguard. Pair it with a sensible --max-redirs cap so a misbehaving or hostile server cannot walk you somewhere unexpected.

Guardrails worth setting in scripts

  • --proto-redir =https to refuse insecure hops during a chain.
  • --max-redirs with a small number to bound the walk.
  • -f (or --fail) so HTTP error codes after the final hop surface as failures.

Credentials, cookies and the cross-host boundary

When a redirect crosses to a different host, curl deliberately drops sensitive headers such as Authorization to avoid leaking your token to a third party. That is safe behaviour, but it surprises people whose API call "stops working" after a redirect. If you genuinely trust the destination, --location-trusted resends those credentials across hosts, but you should reach for it only when you control or fully trust the redirect target. Cookies are a separate concern: state set partway through a chain persists only if you supply a cookie jar with -c and -b, otherwise a session established on hop one is gone by hop two.

Observing the chain with the write-out variables

Following redirects silently is fine until something is wrong and you cannot see why. curl's -w write-out variables turn the chain into data you can log. %{num_redirects} reports how many hops happened, %{url_effective} gives the final landing URL, and timing variables such as %{time_redirect} and %{time_total} show where latency accumulates. In monitoring and geo-testing this is invaluable: a sudden jump in hop count or redirect time often signals a tracking layer, a misconfigured CDN, or region-specific routing you would otherwise miss.

Why redirect testing belongs behind a proxy

Many redirect decisions are location-aware: a site may send visitors from one region to a localised path or a consent gateway. Running curl -L through a proxy means every hop in the chain exits from the same IP, so you observe the destination exactly as a visitor from that location would. This consistency is what makes proxied redirect checks trustworthy for verifying geo-routing or localisation. For repeated or large-scale checks across regions, a value-focused pool such as Cheapest Proxies (https://cheapest-proxies.com/) lets you run these tests without provisioning more capacity than the job needs.

Pros and cons to weigh

Strengths

  • <code>-L</code> resolves the entire chain to the final content in one command
  • Protocol and redirect-count limits give precise control over how far curl will walk
  • Write-out variables make each hop observable and loggable
  • Cross-host credential dropping protects tokens from leaking by default
  • Proxied chains reproduce region-specific redirect behaviour faithfully

Trade-offs

  • Default behaviour silently changes some request methods on certain status codes
  • Auth headers vanishing on cross-host hops confuses integrations that "worked in the browser"
  • Long or circular chains waste time and quota without a cap
  • Relative <code>Location</code> headers can land you on an unexpected host
  • Cookie-dependent flows break unless you explicitly persist a cookie jar

Common mistakes to avoid

  • Forgetting <code>-L</code> entirely and parsing the 3xx body as if it were the page
  • Reaching for <code>--location-trusted</code> on untrusted destinations and leaking credentials
  • Leaving <code>--max-redirs</code> unset in unattended scripts so loops run unbounded
  • Assuming POST survives a redirect without setting the relevant <code>--postNNN</code> option

Before-you-buy checklist

  • Confirm whether the endpoint changes method on redirect and set <code>--post301/302/303</code> if needed
  • Set <code>--max-redirs</code> to a sane cap for any automated job
  • Decide whether HTTPS-only chains are required and add <code>--proto-redir</code>
  • Supply a cookie jar if the flow sets session state mid-chain
  • Log <code>%{url_effective}</code> and <code>%{num_redirects}</code> for auditability
  • Route through a proxy when redirect behaviour depends on region
$

How to get the best value

Right-size the plan

Start on the smallest sensible tier and scale only what proves itself on your real targets.

Type before brand

Pick the proxy type the task needs first — it drives both success rate and cost more than the logo.

Read the fine print

Check traffic limits, rotation rules and what happens on overage before you commit.

Lead with value

Our featured value pick, Cheapest Proxies, is a sensible starting point for affordable comparison.

📖

Key terms explained

Location header
The response header on a 3xx reply that tells the client where to go next.
--location-trusted
A curl option that resends credentials even across a redirect to a different host.
--proto-redir
A curl option restricting which protocols a redirect chain is allowed to use.
url_effective
A write-out variable holding the final URL curl reached after following the chain.
Open redirect
A redirect whose target is attacker-controllable, often abused to disguise malicious destinations.

Why compare before buying?

Following redirects with curl is free, but the moment you do this across regions or at volume, the proxy behind your requests determines what you actually see and how reliably the job runs. Comparing proxy options on value first means your redirect tests and scrapes reflect the right location and stay within budget, instead of paying for an oversized plan or fighting an undersized one.

How we compare

Compare Proxy Zone weighs providers on value, fit and reliability using qualitative judgement — never invented prices, speeds or uptime figures. See our review methodology, or email info@compareproxyzone.com with a correction.

?

Frequently asked questions

Why does curl not follow redirects automatically?

By default curl shows the 3xx response so you can inspect exactly what the server returned without hidden hops; you opt in to following with the -L flag when you want the final content.

What is the difference between -L and -I?

The -L flag tells curl to follow the redirect chain to the final URL, while -I fetches only the headers; combining them as -IL lets you see the headers of every hop in the chain.

How do I stop curl from following too many redirects?

Use --max-redirs with a number, for example --max-redirs 5, so curl stops and reports an error instead of looping endlessly through a long or circular chain.

Why does my POST request turn into a GET after a redirect?

Some status codes cause curl to switch the method on the next hop; use options like --post301, --post302 or --post303 to preserve POST across those redirects.

Can I follow redirects through a proxy?

Yes. Add your proxy with the -x option alongside -L, and every hop in the chain will travel through that proxy, which is ideal for testing how a site behaves from a specific IP or region.

How do I see the final URL curl ended on?

Add -w "%{url_effective}" to print the effective URL after all redirects, which is handy for confirming where a shortened or chained link actually leads.

Compare on value, then decide

For affordable proxies across the main types, our featured value pick is Cheapest Proxies — a strong budget-friendly option worth considering. Check the exact plan before ordering.