Follow Redirects with cURL

By  on  

I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does.

I recently cURL'd the Google homepage and saw the following:

curl google.com

#<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
#<TITLE>301 Moved</TITLE></HEAD><BODY>
#<H1>301 Moved</H1>
#The document has moved
#<A HREF="http://www.google.com/">here</A>.
#</BODY></HTML>

I found it weird that Google does the initial redirect but I still want to get the source of the Google homepage with cURL, as with any site that may do a redirect without you noticing. Luckily it's just a single flag:

curl -L google.com

#<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en">...

The -L flag instructs cURL to follow any redirect so that you reach the eventual endpoint. Those tiny redirects are just noise anyways, right?

Recent Features

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    &#8220;Top&#8221; Watermark Using MooTools

    Whenever you have a long page worth of content, you generally want to add a "top" anchor link at the bottom of the page so that your user doesn't have to scroll forever to get to the top. The only problem with this method is...

  • By
    Image Manipulation with PHP and the GD Library

    Yeah, I'm a Photoshop wizard. I rock the selection tool. I crop like a farmer. I dominate the bucket tool. Hell, I even went as far as wielding the wizard wand selection tool once. ...OK I'm rubbish when it comes to Photoshop.

Discussion

  1. Dan

    Is there a way to do this outside of the command line in PHP?

    • H.Gerber

      The curl-lib in PHP offers an option for that:

      $handle = curl_init();
      ...
      curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
      ...
      curl_exec($handle);
      ...
      
    • Jonny

      And if the url is using/forcing https, set this option before executing the curl:

      curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
      
    • Robert Munro

      Please don’t do that, unless you really don’t care about the content of the download. It essentially says “Ignore the SSL errors if it’s broken – trust the same as you would an unencrypted URL.”

      The data will still be encrypted, but could be coming from a man-in-the-middle, not from where you thing it’s coming from.

  2. The implementation of curl in PHP has an option for that.

    See the following link:
    http://stackoverflow.com/questions/3519939/make-curl-follow-redirects

  3. A

    if you liked curl, you’ll love lynx..

  4. I hit an issue with not following redirects myself, but this was a download from a GitHub release download that was redirecting to a S3 bucket. Broke a CLI out in the wild…

    In this case, I’m not sure it’s noise (to get at your closing question). I was intentionally not following redirects, as one should not expect this to be happening with GitHub downloads…

    I have a support ticket in to see what’s up with this very recent change. Maybe it was a DevOops?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!