Node.js code for Curl Follow Redirect ExampleThis Node.js code snippet was generated automatically for the Curl Follow Redirect example.
<< Back to the Curl Follow Redirect example
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.
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 Syntax
The general form of the Curl Follow Redirect command is as follows:
Curl Redirect Example
Curl example of following a redirect to another host with user credentials and --location-trust flag.:
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.
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.
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.