How can I use Curl to follow redirects?

You can use the -L or --location option in Curl to follow redirects. It means if the server reports that the requested page has moved to a different location, Curl will automatically make a new request to the new location.

Here is an example:

curl -L http://example.com

In the example above, curl will follow any redirect until it reaches the final destination. Be aware that following redirects without limit can be dangerous, as it opens up the possibility for infinite redirect loops.

Additionally, you can use the -I option to fetch the headers only, in case you want to see the redirect chain:

curl -LI http://example.com

This command will reveal the entire chain of redirects, showing each HTTP response header. This can be useful to debug redirect problems.

By default, curl will only follow HTTP 3xx redirects with GET method. If you want curl to follow HTTP POST method redirects, you can use the --post301, --post302 and --post303 options:

curl -L --post301 --post302 --post303 -d "param=value" http://example.com

This command will make curl follow any HTTP 301, 302, 303 redirect with a POST method. The -d option is used to send data in a POST request to the server.

Remember that using Curl to follow redirects should be done with caution. Always ensure you trust the source and understand the potential security implications.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon