The -L
or --location
flag in Curl
is used to handle HTTP redirects.
When you make a request to a website or a web server, sometimes the resource you requested might have been moved to a different location. In this case, the server will send back a response with a status code in the 300 range, usually 301 (Moved Permanently) or 302 (Found), indicating that the resource has been redirected to a new URL. The new URL will be included in the Location
header of the HTTP response.
By default, Curl
does not follow these redirects, meaning it will just return the initial response from the server with the redirect status code. If you want Curl
to automatically follow the redirect and make a new request to the new URL, you can use the -L
or --location
flag.
For example:
curl -L http://example.com
In this example, if http://example.com
returns a 301 or 302 status code with a new URL in the Location
header, Curl
will automatically make a new request to that URL and return the response.
It's important to note that -L
will only cause Curl
to follow redirects if the request method is GET
or HEAD
. For other methods, such as POST
, Curl
will not follow the redirect unless you also use the --post301
or --post302
options.
Finally, if you want Curl
to follow redirects and keep the request method the same (i.e., a POST
request remains a POST
request after the redirect), you can use the -L
flag in combination with the --post301
, --post302
and --post303
options:
curl -L --post301 --post302 --post303 http://example.com
This will cause Curl
to follow all redirects and maintain the original request method.