Get final URL after curl is redirected
curl
's -w
option and the sub variable url_effective
is what you arelooking for.
Something like
curl -Ls -o /dev/null -w %{url_effective} http://google.com
More info
-L Follow redirects-s Silent mode. Don't output anything-o FILE Write output to <file> instead of stdout-w FORMAT What to output after completion
More
You might want to add -I
(that is an uppercase i
) as well, which will make the command not download any "body", but it then also uses the HEAD method, which is not what the question included and risk changing what the server does. Sometimes servers don't respond well to HEAD even when they respond fine to GET.
Thanks, that helped me. I made some improvements and wrapped that in a helper script "finalurl":
#!/bin/bashcurl $1 -s -L -I -o /dev/null -w '%{url_effective}'
-o
output to/dev/null
-I
don't actually download, just discover the final URL-s
silent mode, no progressbars
This made it possible to call the command from other scripts like this:
echo `finalurl http://someurl/`
as another option:
$ curl -i http://google.comHTTP/1.1 301 Moved PermanentlyLocation: http://www.google.com/Content-Type: text/html; charset=UTF-8Date: Sat, 19 Jun 2010 04:15:10 GMTExpires: Mon, 19 Jul 2010 04:15:10 GMTCache-Control: public, max-age=2592000Server: gwsContent-Length: 219X-XSS-Protection: 1; mode=block<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"><TITLE>301 Moved</TITLE></HEAD><BODY><H1>301 Moved</H1>The document has moved<A HREF="http://www.google.com/">here</A>.</BODY></HTML>
But it doesn't go past the first one.