How to use curl to follow redirects automatically?

Published on Aug. 22, 2023, 12:19 p.m.

To use curl to follow redirects automatically, you can use the -L or --location flag in your curl command.

curl -L <URL>

In this example, we’re using the -L flag to instruct curl to follow any redirects that are returned by the server in response to the initial request.

Alternatively, you can use the -i or --include flag to include the response headers in the output. This can be useful in determining whether a redirect occurred and where it redirected to.

curl -i <URL>

In this example, we’re using the -i flag to include the headers in the output.

Note that some clients or servers may return a large number of redirects, which can lead to an infinite loop if curl is set to follow them automatically. In such cases, you may need to adjust the maximum number of redirects that curl will follow or handle the redirects manually.

Also, ensure you update the <URL> as per your specific requirements.

Tags: