Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to check where a single URL redirects. An example of that could be a link from Google's search result page (where a click always goes through Google server).

Can I do that with curl?

share|improve this question
up vote 7 down vote accepted

Try this:

$ LOCATION=`curl -I http://raspberrypi.stackexchange.com/a/1521/86 | perl -n -e '/^Location: (.*)$/ && print "$1\n"'`
$ echo "$LOCATION"

Google Redirects

Google redirect URLs are slightly different. They return a Javascript redirect, which could easily be processed, but why not process the original URL and for go curl all together?

$ URL="http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CFAQFjAA&url=http%3A%2F%2Fwww.raspberrypi.org%2F&ei=rv8oUODIIMvKswa4xoHQAg&usg=AFQjCNEBMoebclm0Gk0LCZIStJbF04U1cQ"
$ LOCATION=`echo "$URL" | perl -n -e '/url=([a-zA-Z0-9%\.]*)/ && print "$1\n"'`
$ echo "$LOCATION"
$ echo "$LOCATION" | perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'


  1. For url decode...
share|improve this answer
How about special URLs with special characters (like Google's redirects), e.g.: google.com/… – ΔλЛ Aug 13 '12 at 13:24
@REACHUS The redirect is encoded into the query string of the URL given. – Alex Chamberlain Aug 13 '12 at 13:28
@REACHUS No worries - you can probably combine the 2 perl statements. – Alex Chamberlain Aug 13 '12 at 14:12

There is an even easier way

curl -w "%{url_effective}\n" -I -L -s -S $URL -o /dev/null

it would print


for URL

share|improve this answer
That uses more time and bandwidth though, since you're downloading the second page as well. – unhammer May 6 '15 at 12:30
@unhammer You are right, updated my answer to do head requests only. – ismail Aug 7 '15 at 8:11

or try this

curl -s -o /dev/null -I -w "HTTP_CODE: %{http_code}\nREDIRECT_URL: %{redirect_url}\n" http://raspberrypi.stackexchange.com/a/1521/86
share|improve this answer
How about special URLs with special characters (like Google's redirects), e.g.: google.com/… – ΔλЛ Aug 13 '12 at 13:25
just put the url in single quotes, so that your shell ignores the special characters in the link. But the link you have stated does not redirect to another uri, the status code of the response is 200 and not 3xx. The uri you look for is hidden in the uri itself respectively in the content of the response. For further examination you can study the header of the response with curl -s -I 'http://yoururl' and the content of the response with curl -s 'http://yoururl' (you will see that google use a simple javascript for the redirection). – user1146332 Aug 13 '12 at 14:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.