When using the cURL tool to access web pages or APIs, it's not uncommon to encounter HTTP redirects. These are server responses that tell the client to look at a different URL, often because the content has moved or the URL structure has changed. This could be a temporary redirect (HTTP status 302) or a permanent one (HTTP status 301).
While browsers automatically follow these redirects, cURL does not, unless instructed to. Instead, it will display the initial redirect response from the server. For many tasks, especially automating web processes or accessing APIs, it's important to be able to follow these redirects seamlessly.
The good news is, cURL offers a simple flag, -L or –location, to instruct it to follow any HTTP redirects it encounters. This ensures that you get the final content, irrespective of any intermediate redirects.
$ $ curl -i http://simplified.guide HTTP/1.1 301 Moved Permanently Server: awselb/2.0 Date: Sat, 09 Sep 2023 13:46:32 GMT Content-Type: text/html Content-Length: 134 Connection: keep-alive Location: https://www.simplified.guide:443/ <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> </body> </html>
$ curl -L http://simplified.guide <!DOCTYPE html> <html lang="en"> <head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-SZLRMNXV54"></script> <script> ##### snipped
-L, --location If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
$ curl -L --max-redirs 10 http://simplified.guide
Be cautious when increasing the limit of --max-redirs to avoid potential infinite redirect loops.
With the -L flag in place, you can confidently use cURL in scripts or automation processes, knowing that it will always retrieve the final content regardless of any redirects in the way.
Comment anonymously. Login not required.