When you execute a cURL request to an HTTP server and if the page has been moved to another location then you will get an 3XX HTTP response.
Status Code | Meaning |
---|---|
300 | Multiple Choices |
301 | Moved Permanently |
302 | Found |
303 | See Other |
304 | Not Modified |
307 | Temporary Redirect |
308 | Permanent Redirect |
If you want your cURL command to follow all the redirects then you need to make use of the location flag -L or --location
Let's look at some examples, first, we just make use of -I flag to display response headers for a URL that is redirected.
% curl -I http://code2care.org
HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Jul 2023 11:37:56 GMT
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Wed, 05 Jul 2023 12:37:56 GMT
Location: https://code2care.org/
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 7e1f519edd19f498-BOM
alt-svc: h3=":443"; ma=86400
As you can see there was a 301 Moved Permanently response and cURL did not follow the redirect.
Now let's make use of the -L flag for the same request.
% curl -I -L http://code2care.org
HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Jul 2023 11:42:26 GMT
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Wed, 05 Jul 2023 12:42:26 GMT
Location: https://code2care.org/
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 7e1f5834a95f0e1c-BOM
alt-svc: h3=":443"; ma=86400
HTTP/2 200
date: Wed, 05 Jul 2023 11:42:26 GMT
content-type: text/html; charset=UTF-8
cache-control: public, max-age=2592000
content-security-policy: upgrade-insecure-requests;
display: staticcontent_sol
pagespeed: off
response: 200
As you may see that the redirect to https://code2care.org was followed and we got a HTTP/2 200 OK message.
Facing issues? Have Questions? Post them here! I am happy to answer!
- cURL Option to show HTTP Response Status Code
- URL Encode cURL HTTP GET\POST Data with Examples
- Capture cURL Request Output to a File
- Fix: Curl No Match Found Error
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer
- Send JSON Data as Payload using cURL HTTP POST Request
- cURL: Show Request and Response Headers
- Get cURL command from Chrome Inspect Network HTTP URL
- Save cURL Command Output to a external file
- cURL DELETE Request with Examples
- How to URL Encode Data for a cURL Request Parameters
- cURL Example using IP (IPv4 and IPv6) address and Port
- How to convert a Postman request to cURL Command
- [fix] command not found curl
- How to Pretty Print cURL JSON Output in Terminal
- How to follow redirects in cURL Command Request
- How to pass authorization header using cURL
- cURL -x Use of HTTP Proxy Server Examples
- Downloading Google Chrome using cURL Command
- How to get cURL Command to run in verbose mode?
- List of 28 Protocols supported by cURL with Examples
- Hide cURL Outputs and Errors on Terminal
- cURL -d Option with Examples
- How to show SSL Certificate details using cURL Command
- Perform Basic Authentication using cURL with Examples
- How to add a Delay of a Few Seconds in Java Program - Java
- How to save IntelliJ IDE Console logs to external log file - Android-Studio
- [Fix] Steam Friends Network Unreachable Error - HowTos
- Java StringJoiner vs StringBuilder - Java
- Notepad++ is about to exit prompt message - NotepadPlusPlus
- Top 5 Fonts for Windows Notepad - Windows
- Mac: Change Screenshot Location - MacOS
- How to End a Program in Python - Python