When using the cURL tool to access web pages or APIs, it's not uncommon to encounter HTTP redirects. These are server responses that tell the client to look at a different URL, often because the content has moved or the URL structure has changed. This could be a temporary redirect (HTTP status 302) or a permanent one (HTTP status 301).

While browsers automatically follow these redirects, cURL does not, unless instructed to. Instead, it will display the initial redirect response from the server. For many tasks, especially automating web processes or accessing APIs, it's important to be able to follow these redirects seamlessly.

The good news is, cURL offers a simple flag, -L or –location, to instruct it to follow any HTTP redirects it encounters. This ensures that you get the final content, irrespective of any intermediate redirects.

Steps to make cURL automatically follow redirects:

  1. Open the terminal.
  2. Run curl against a URL with a known redirect.
    $ $ curl -i http://simplified.guide
    HTTP/1.1 301 Moved Permanently
    Server: awselb/2.0
    Date: Sat, 09 Sep 2023 13:46:32 GMT
    Content-Type: text/html
    Content-Length: 134
    Connection: keep-alive
    Location: https://www.simplified.guide:443/
    <head><title>301 Moved Permanently</title></head>
    <center><h1>301 Moved Permanently</h1></center>
  3. Use the -L option to instruct cURL to follow redirects.
    $ curl -L http://simplified.guide
    <!DOCTYPE html>
    <html lang="en">
                <!-- Google tag (gtag.js) -->
            <script async src="https://www.googletagmanager.com/gtag/js?id=G-SZLRMNXV54"></script>
    ##### snipped
    -L, --location
           If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
  4. For multiple redirects, cURL will follow up to a maximum of 50 redirects by default to avoid infinite loops. Adjust this using the --max-redirs option.
    $ curl -L --max-redirs 10 http://simplified.guide

    Be cautious when increasing the limit of --max-redirs to avoid potential infinite redirect loops.

  5. Verify the content after redirect. If everything is set up correctly, the content from the final URL after all redirects will be displayed.

With the -L flag in place, you can confidently use cURL in scripts or automation processes, knowing that it will always retrieve the final content regardless of any redirects in the way.

Discuss the article:

Comment anonymously. Login not required.