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
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    MooTools Equal Heights Plugin:  Equalizer

    Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're...

  • By
    Animated Progress Bars Using MooTools: dwProgressBar

    I love progress bars. It's important that I know roughly what percentage of a task is complete. I've created a highly customizable MooTools progress bar class that animates to the desired percentage. The Moo-Generated XHTML This DIV structure is extremely simple and can be controlled...

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!