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

RedirectCurlCommand Line-Interface

Redirect Problem Overview


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?

Redirect Solutions


Solution 1 - Redirect

Use the location header flag:

curl -L <URL>

Solution 2 - Redirect

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

> 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.

Solution 3 - Redirect

I had a similar problem. I am posting my solution here because I believe it might help one of the commenters.

For me, the obstacle was that the page required a login and then gave me a new URL through javascript. Here is what I had to do:

curl -c cookiejar -g -O -J -L -F "j_username=username" -F "j_password=password" <URL>

Note that j_username and j_password is the name of the fields for my website's login form. You will have to open the source of the webpage to see what the 'name' of the username field and the 'name' of the password field is in your case. After that I go an html file with java script in which the new URL was embedded. After parsing this out just resubmit with the new URL:

curl -c cookiejar -g -O -J -L -F "j_username=username" -F "j_password=password" <NEWURL>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1592380View Question on Stackoverflow
Solution 1 - RedirectNathan KuchtaView Answer on Stackoverflow
Solution 2 - RedirectfreedevView Answer on Stackoverflow
Solution 3 - Redirectuser3817445View Answer on Stackoverflow