Get final URL after curl is redirected

前端 未结 11 1986
清酒与你
清酒与你 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 07:55

    The parameters -L (--location) and -I (--head) still doing unnecessary HEAD-request to the location-url.

    If you are sure that you will have no more than one redirect, it is better to disable follow location and use a curl-variable %{redirect_url}.

    This code do only one HEAD-request to the specified URL and takes redirect_url from location-header:

    curl --head --silent --write-out "%{redirect_url}\n" --output /dev/null "https://""goo.gl/QeJeQ4"
    

    Speed test

    all_videos_link.txt - 50 links of goo.gl+bit.ly which redirect to youtube

    1. With follow location

    time while read -r line; do
        curl -kIsL -w "%{url_effective}\n" -o /dev/null  $line
    done < all_videos_link.txt
    

    Results:

    real    1m40.832s
    user    0m9.266s
    sys     0m15.375s
    

    2. Without follow location

    time while read -r line; do
        curl -kIs -w "%{redirect_url}\n" -o /dev/null  $line
    done < all_videos_link.txt
    

    Results:

    real    0m51.037s
    user    0m5.297s
    sys     0m8.094s
    

提交回复
热议问题