Curl Follow Redirect(s)

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: August 3, 2023

The short answer

To make curl follow a redirect, you can use the -L flag (short for --location) as follows:

Bash
$ curl -L <url>

When sending an HTTP request with curl, the server may respond with an HTTP 3XX (e.g. 300, 301, 302), indicating that the requested resource has been moved to a different location (i.e. URL). This new location can be found in the Location header of the response.

By default, curl will not follow this redirect.

To make curl follow a redirect and get the requested resource from the new URL, you can use the -L flag as shown above.

This flag is often used by developers to get detailed information about how the server processed their request. For example, when performing basic authentication or sending JSON data.

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering curl follow redirect in the AI Command Search will prompt a curl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Following multiple redirects with curl

When using -L, curl will follow up to 50 sequential redirects by default.

Extend the maximum allowed curl redirects

A poorly configured server has the potential to cause curl to get stuck in an infinite loop by sending back a response that redirects to the same URL. To avoid this, you can use the --max-redirs flag to limit the number of redirects curl follows:

Bash
$ curl -L --max-redirs 5 <url>

If you want to also print the redirects, you can do so by logging the request and response headers while in the process of being redirected. Combine curl with the -v flag (short for --verbose) as follows:

Bash
$ curl -v -L <url>

Note that when curl follows an HTTP 301, 302, or 303 redirect, and the original request method is not a plain GET (e.g. it is a POST or PUT), the ensuing requests will automatically be converted into a GET, which means that the original body of the request will not be passed along.

curl and 301, 302, etc. redirects

In the context of following redirects, curl treats all redirect requests the same regardless of 3XX status code.

An HTTP 301 redirect indicates that the requested resource has been permanently moved to the URL specified in the Location header of the response. This redirect is often used when a website or API is redesigned or restructured, and some of its URLs have changed.

An HTTP 302 redirect, on the other hand, indicates that the requested resource has been temporarily moved to the URL specified in the Location header of the response. This redirect is often used when a website or API is undergoing short-term changes, such as maintenance operations.

Despite these differences, curl invoked with -L will simply carry on to make the ensuing request.

Avoiding common pitfalls

Verifying a broken redirect chain

curl may sometimes fail to follow a redirect sent by a server due to a broken or invalid URL. To verify that the redirect target is valid and accessible, you can use the -I (capital “i”) flag to only fetch the HTTP headers without including the content.

Bash
$ curl -I <url>

Forwarding credentials to another host

When performing an authentication with curl, the provided credentials will not be forwarded alongside the request if a redirect takes curl to a different host than the initial one. To bypass this behavior, you can use the --location-trusted flag as follows:

Bash
$ curl --location-trusted -u user:password <url>

Note that you should be particularly careful when using this flag as it may introduce a security breach if the redirect forwards your credentials to an untrusted or malicious host.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.