Get final URL after curl is redirected

前端 未结 11 1933
清酒与你
清酒与你 2020-12-04 07:38

I need to get the final URL after a page redirect preferably with curl or wget.

For example http://google.com may redirect to http://www.google.com

11条回答
  •  北海茫月
    2020-12-04 08:03

    curl's -w option and the sub variable url_effective is what you are looking 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  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.

提交回复
热议问题