Following Redirects with Curl

By default, Curl does not follow redirects and displays the content of the 300x page (if any). To follow redirects with Curl, you need to use the -L or --location command-line option. The server indicates that the resource has moved to a new location using the 3XX response code and provides the new address with the Location HTTP header. In this Curl Follow Redirects example, we send a request to www.reqbin.com, which redirects to reqbin.com. Click Run to execute the Curl Follow Redirects example online and see the results.
Following Redirects with Curl Run
curl https://www.reqbin.com/echo -L
Updated: Viewed: 21086 times

What is Curl?

Curl is a command-line tool that can be used to transfer data to/from a server using over 25+ protocols, including HTTP, HTTPS and FTP. Curl is used for debugging network requests and API calls and has built-in support for web forms, SSL, HTTP Cookies. Curl works on Mac, Windows, Linux, and many other platforms.

What is HTTP Redirect?

HTTP Redirect (or redirect for short) redirects visitors and search engines from one URL to another. Redirection is one of the basic building blocks of the Internet and is implemented as part of the HTTP protocol The concept of HTTP Redirect was introduced and documented already in the first specification of the HTTP protocol (RFC 1945). HTTP redirects are used, for example, when you move your site to a new, more attractive domain name and want to automatically redirect all users from the old domain name to the new one. HTTP redirects are also used when you want to redirect all visitors from the HTTP version of your pages to the more secure HTTPS version or when the page content has been moved to a new address or deleted. If the server wants to redirect the HTTP client to a new URL, it must return one of the 300x status codes and the new address in the HTTP Location header. The address itself can be either absolute or relative.

HTTP Redirect Example
HTTP/1.1 301 Moved Permanently
Location: https://reqbin.com/
Content-Length: 0

How to make Curl follow redirects?

To follow redirect with Curl, use the -L or --location command-line option. This flag tells Curl to resend the request to the new address. When you send a POST request, and the server responds with one of the codes 301, 302, or 303, Curl will make the subsequent request using the GET method. For other 300x status codes, Curl will resend the subsequent request using the same unmodified HTTP method. This behavior can be changed using one of the --post301, --post302, or --post303 flags. When authentication is used, Curl only sends its credentials to the first host. If Curl goes to a different host when redirecting, it will not provide user credentials to the new host (you can change this behavior with the --location-trust flag). To limit the number of following redirects, you can use the --max-redirs command-line option.

Curl Redirect Example
curl -L --max-redirs 5 //www.reqbin.com/echo

Curl Redirect Syntax

The general form of the Curl Follow Redirect command is as follows:

Curl Redirect Syntax using -L option
curl -L [URL]

Curl Redirect Example

Curl example of following a redirect to another host with user credentials and --location-trust flag.:

Advanced Curl Follow Redirect Example
curl https://reqbin.com/echo
   -L
   --location-trusted
   -u "user:password"

How to set the maximum number of allowed redirects?

When redirects are enabled with the -L command-line options, Curl can perform up to 50 redirects. To remove the maximum number of redirects and make it limitless, you can set this parameter to -1 using the --max-redirs command-line option. You can also reduce the maximum number of allowed redirects by decreasing the parameter value, for example, to 10. For instance, in Google Chrome and Mozilla Firefox, the maximum redirect limit is 20. The maximum redirect limit helps Curl avoid the risk of falling into an infinite loop.

Set the maximum number of redirects to 10
curl -L --max-redirs 10 [URL]

Set unlimited redirects
curl -L --max-redirs -1 [URL]

How to make Curl not change POST to GET when following 301 and 302 redirects?

You can tell Curl not to change the POST request method to GET for 301, 302, and 303 redirects using one of the following flags: --post301, --post302, or --post303.

Do 301 POST Redirect
curl -L --post301 [URL]

How do I authenticate the user when the server redirects to a different hostname?

When you send requests to the server using Curl, you can also provide the server with user credentials. But if the server redirects Curl to a different host, Curl will not send user credentials to the new address for security reasons. If you still want to send user credentials to the following hostnames, you can use the --location-trust command line parameter to do so.

Redirect to another host names with Curl
curl [URL]
   --location
   --location-trusted
   --user "user:password"

See also

Generate Code Snippets for Curl Follow Redirect Example

Convert your Curl Follow Redirect request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the ReqBin code generator.