50% OFF Residential Proxies for 9 months — use code IPR50 at checkout

Get The Deal
Back to blog

How to Follow Redirects Using cURL: A Practical Guide for Developers

Karolis Toleikis

Last updated -
How to

Key Takeaways

  • By default, cURL doesn’t follow redirects.

  • Use -L to make cURL follow redirects, and pair with -I, -v, --max-redirs, or -s for more control.

  • Define your needs well before using redirect settings to make sure they fit your flow.

Ready to get started?

Register now

Handling redirect responses when working with cURL may seem confusing at first, but with the proper guidance, it’s not difficult to do.

Here you will learn how HTTP redirects work, why cURL does not follow them by default, and how to make it work properly. You’ll get practical tips if you’re building a simple client, debugging HTTP requests, or doing something else entirely.

How cURL Handles Redirects by Default

When you run curl https://iproyal.com on a URL that responds with HTTP redirects, here’s what happens:

$ curl https://iproyal.com
<!doctype html>
<html>
<head><title>Redirecting…</title></head>
<body>
Redirecting to <a href="https://www.iproyal.com/">https://www.iproyal.com/</a>
</body>
</html>

In this output, you see the HTML from the redirecting server, which includes a link to the new location, but cURL does not automatically follow it by default to give you control over the request method and HTTP headers.

It treats each transfer as a distinct request and avoids silent navigation between URLs. This behavior helps protect you from unwanted HTTP redirects and loss of clarity about what your initial cURL command requested, including awareness of the original HTTP method used, which might otherwise be changed automatically when redirects are followed with the -L flag.

How to Make cURL Follow Redirects

To start following redirects with cURL, use the -L flag, which stands for --location. This flag tells cURL to follow the Location: header in the response and keep going until it reaches the final destination:

curl -L https://iproyal.com

After Using -L

$ curl -L https://iproyal.com
<!doctype html>
<html>
<head><title>Example Domain</title></head>
<body>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples...</p>
</body>
</html>

Now, cURL follows the redirect chain and returns the content you need at the final location.

When and Why to Use It

Using -L is useful when working with:

  • Shortened URLs that point to the final destination.
  • Login pages that redirect to dashboards.
  • Scripts that handle file downloads .
  • APIs or websites that return 301/302 status codes.

It’s especially helpful in tasks like GET request and POST request , where following the chain ensures you hit the intended endpoint. If you’re modifying headers or using a custom user agent, the flag ensures those settings persist throughout the redirect chain.

If you’re unsure how to deal with user agents, here’s a full guide on setting up user agents with cURL .

Useful cURL Flags When Handling Redirects

Once you’ve added -L to make cURL follow redirects, you can combine it with other flags to get more control. These flags help you inspect headers, debug issues, limit how far redirects will go, or keep the output clean.

-I: Headers Only

The -I header tells cURL to fetch only the HTTP headers from a URL, not the body:

curl -I -L https://iproyal.com

It’s useful when you just want to see where the URL redirects, what server it’s using, or what the Location: field says in a redirect chain. It’s often used in testing scripts to check if a page returns expected status codes or HTTP redirects.

Pair this with -L to see the headers for each redirected step, especially if dealing with multiple redirects.

Uppercase ‘i’ letter, not lowercase ’L’.

-v: Verbose Mode for Debugging

Using -v gives you a detailed view of what’s happening behind the scenes:

curl -v -L https://iproyal.com

You’ll see each request and response, including the request method, status codes, headers, and the redirect path. It helps trace why a redirect failed or how many steps the final result took.

If you’re customizing the user agent or using cURL proxies , verbose output shows whether those options apply through the entire chain.

--max-redirs: Limit Redirects

Some sites cause loops or too many hops. Use --max-redirs to manage the number of redirects cURL follows:

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

If the redirect count exceeds the limit, cURL stops and shows an error. This prevents infinite loops and protects your scripts. In API automation or testing environments, it ensures your HTTP client doesn’t waste time chasing bad HTTP redirects.

-s: Silent Mode

The -s flag hides the progress meter and most error messages:

curl -s -L https://iproyal.com

This is great for clean output when you just need the body of the response or want to log it somewhere. It’s perfect for background tasks or command-line tools.

These flags let you tailor how cURL redirects behave based on your needs. Combine them smartly to debug, inspect, and limit redirects with cURL effectively. For example, in scripts that require credentials or headers, this control is essential to keep your authentication logic reliable.

Conclusion

Mastering how cURL handles redirects helps you build smoother scripts and debug networking issues easily. Use -L to start following redirects with cURL, adjust verbosity with -v, inspect headers using -I, and control behavior and the number of redirects cURL follows with --max-redirs and -s.

FAQ

Does cURL follow redirects automatically?

No. The default cURL command doesn’t follow HTTP redirects. To override that, you need to add the -L flag.

How to stop cURL from following redirects?

Simply remove the -L flag, since that’s the only thing that makes cURL follow redirects. In its most basic syntax, cURL doesn’t do it.

How do I ignore certificates in cURL when following redirects?

Combine -L with -k (or --insecure). For example:

curl -L -k https://iproyal.com

This tells cURL to ignore SSL certificate errors even when redirecting.

How to debug redirects in cURL?

Add -v to your command:

curl -v -L https://iproyal.com

It shows each redirect step and all headers and request methods used.

Create Account

Author

Karolis Toleikis

CEO

Karolis thrives on transforming ideas into successful projects, focusing on what attracts early customers and identifying market gaps. Thanks to his vast background in IT and programming, he brings a deep technical understanding to his leadership, ensuring seamless operations and long-term stability. Karolis takes a big-picture approach, continuously refining company processes and keeping teams focused on strategic goals. Away from the office, he’s a massive padel enthusiast, believing that a day without a match is a day wasted.

Learn More About Karolis Toleikis
Share on

Related articles