Get Redirect URL with cURL

By  on  

URL redirects can be glorious or annoying depending on which side of them you are on and which side you want to be on.  Redirects are helpful for vanity URLs (useful in advertising) but sometimes they're annoying in that they could potentially break your code if you encounter a redirect you weren't anticipating.

The useful and amazing cURL command line utility allows you to fetch an address and return its final destination URL:

# "/css" doesn't exist but WordPress tries to find the closest match
# and then redirects to it ("/css-animation-callback")

curl -Ls -w %{url_effective} -o /dev/null https://davidwalsh.name/css

# Outputs: https://davidwalsh.name/css-animation-callback

The url_effective variable is what we're after.  The L directive tells cURL to follow redirects, the s directive tells cURL to be silent (i.e. not output the page contents).  You provide the URL, cURL follows it to its endpoint -- easy!

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    Jack Rugile’s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

  • By
    FileReader API

    As broadband speed continues to get faster, the web continues to be more media-centric.  Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it).  And every social service does...

Discussion

  1. J-K. Solbakken

    Follow a URL using JavaScript: https://www.npmjs.com/package/linkfollower

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