Redirects represent a core component of HTTP protocols, enabling requests to be forwarded to alternative resources that contain the requested data.
cURL commands do not follow redirects automatically by default. As an example, consider attempting to request httpbin.dev/absolute-redirect/:n, which redirects the request a specified number of times:
curl https://httpbin.dev/absolute-redirect/6
Running this command will produce no output, since the request does not continue to its ultimate destination. To enable cURL to follow redirects, we can utilize the -L or –location flag:
curl -L https://httpbin.dev/absolute-redirect/6
This cURL request will follow all 6 redirects and ultimately return a response:
{
"args": {},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"gzip"
],
"Host": [
"httpbin.dev"
],
"User-Agent": [
"curl/8.4.0"
]
},
"url": "https://httpbin.dev/get"
}
When using the -L or –location cURL options, redirect following is enabled with a default maximum limit of 50 redirects. To modify this redirect limit, we can employ the –max-redirs cURL option:
curl -L https://httpbin.dev/absolute-redirect/51 --max-redirs 51
For more comprehensive information about cURL, refer to our previous guide.