Lets suppose that we have 3 links: link1, link2, link3. link1 redirects to link2 and link2 redirects to link3. So how to see that with curl?


You can see the HTML headers using -I. If the redirect is a meta-refresh it would should up in this way as a header.

lamp@oort ~ $ curl -I http://google.com<br>
HTTP/1.1 301 Moved Permanently<br>
Location: http://www.google.com/<br>
Content-Type: text/html; charset=UTF-8<br>
Date: Thu, 21 Nov 2013 14:59:13 GMT<br>
Expires: Sat, 21 Dec 2013 14:59:13 GMT<br>
Cache-Control: public, max-age=2592000<br>
Server: gws<br>
Content-Length: 219<br>
X-XSS-Protection: 1; mode=block<br>
X-Frame-Options: SAMEORIGIN<br>
Alternate-Protocol: 80:quic

If the redirect is happening via PHP, you could detect this by comparing where the browser is going vs. where it actaully goes... There are a bunch of ways to do this with Python, JS, etc. One project which may be interesting to you is phantomjs, a scriptable headless browser.


Try this :

for link in link1 link2 link3; do
    curl -Is "$link" | awk '/Location/{print $2}'

Or using :

for link in link1 link2 link3; do
    printf '%s\n%s\n\n%s\n' 'HEAD / HTTP/1.1' "Host: $link" 'Connexion:close' |
    netcat $link 80 | awk '/Location/{print $2}'

From man curl:

   -w, --write-out <format>
          Defines what to display on stdout after a completed and
          successful operation.


          redirect_url   When an HTTP request was made without -L to
                         follow redirects, this variable will show the 
                         actual URL a redirect would take you to.
                         (Added in 7.18.2)

So probably curl -w "%{redirect_url}" link1 will give you the first redirection url.

Maybe something like this works for you:

while [ -n "${URL}" ]
    echo $URL
    URL=$(curl -sw "\n\n%{redirect_url}" "${URL}" | tail -n 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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