A simple redirect resolver

December 15, 2010 at 8:53 PM

Did you know that curl has an option to automatically follow HTTP redirects? Combined with the HEAD method, this can be used to quickly trace the end destination in a chain of redirecting URLs:

#!/bin/sh
(echo $1 && curl -LIs "$1" | grep '^Location' | cut -d' ' -f2) | cat -n

The meat of the work is in the curl options:

The subsequent grep/cut/cat combo serve to dice up the headers that are sent back and turn them into a pretty list of redirect locations.