Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to follow redirects with command line cURL?

I know that in a php script:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

will follow redirects. Is there a way to follow redirects with command line cURL?

like image 642
user1592380 Avatar asked Aug 27 '13 20:08

user1592380


People also ask

How do I follow redirects with curl?

To follow redirect with Curl, use the -L or --location command-line option. This flag tells Curl to resend the request to the new address. When you send a POST request, and the server responds with one of the codes 301, 302, or 303, Curl will make the subsequent request using the GET method.

What happens when you curl a URL from the command line?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.

How do I use the curl command to request?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

Does Google crawl redirects?

Does Google crawl and index redirects? No, it does not. This means that if you redirect from one page to another, the content on the original page will not get indexed. Only the target URL will be crawled and indexed by the search engine.


2 Answers

Use the location header flag:

curl -L <URL>

like image 55
Nathan Kuchta Avatar answered Sep 30 '22 16:09

Nathan Kuchta


As said, to follow redirects you can use the flag -L or --location:

curl -L http://www.example.com 

But, if you want limit the number of redirects, add the parameter --max-redirs

--max-redirs <num> 

Set maximum number of redirection-followings allowed. If -L, --location is used, this option can be used to prevent curl from following redirections "in absurdum". By default, the limit is set to 50 redirections. Set this option to -1 to make it limitless. If this option is used several times, the last one will be used.

like image 31
freedev Avatar answered Sep 30 '22 17:09

freedev