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!
More Posts related to cURL,
- Hide cURL Outputs and Errors on Terminal
- cURL DELETE Request with Examples
- Downloading Google Chrome using cURL Command
- List of 28 Protocols supported by cURL with Examples
- Get the Size of HTTP Response using cURL Command (Content Length)
- How to convert a Postman request to cURL Command
- How to URL Encode Data for a cURL Request Parameters
- Get cURL command from Chrome Inspect Network HTTP URL
- Fetch only content-type using cURL Command
- cURL -d Option with Examples
- cURL Option to show HTTP Response Status Code
- URL Encode cURL HTTP GET\POST Data with Examples
- How to get cURL Command to run in verbose mode?
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer
- [fix] command not found curl
- How to Pretty Print cURL JSON Output in Terminal
- Capture cURL Request Output to a File
- List of common cURL Error Codes
- How to pass authorization header using cURL
- Perform Basic Authentication using cURL with Examples
- Trace using cURL Command Example
- Efficient way to perform HTTP cURL GET/POST Requests with Payload
- Save cURL Command Output to a external file
- cURL Example using IP (IPv4 and IPv6) address and Port
- How to follow redirects in cURL Command Request
More Posts:
- How to Clear Cache for a website (URL) in Safari for Mac - MacOS
- How to remove old 404 pages ulrs from Google crawler - Html
- 5 Programming Languages to Learn in the Year 2021 - News
- Python: Pandas Rename Specific Column names in DataFrame Example - Python
- How to know the version of Ubuntu - Ubuntu
- Strikethrough Text in Excel for Mac - MacOS
- Fix: chroot: failed to run command /bin/bash: No such file or directory - Bash
- [docker] Error response from daemon: No such container - Docker