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.
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!