Get final URL after curl is redirected

前端 未结 11 1930
清酒与你
清酒与你 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:06

    curl can only follow http redirects. To also follow meta refresh directives and javascript redirects, you need a full-blown browser like headless chrome:

    #!/bin/bash
    real_url () {
        printf 'location.href\nquit\n' | \
        chromium-browser --headless --disable-gpu --disable-software-rasterizer \
        --disable-dev-shm-usage --no-sandbox --repl "$@" 2> /dev/null \
        | tr -d '>>> ' | jq -r '.result.value'
    }
    

    If you don't have chrome installed, you can use it from a docker container:

    #!/bin/bash
    real_url () {
        printf 'location.href\nquit\n' | \
        docker run -i --rm --user "$(id -u "$USER")" --volume "$(pwd)":/usr/src/app \
        zenika/alpine-chrome --no-sandbox --repl "$@" 2> /dev/null \
        | tr -d '>>> ' | jq -r '.result.value'
    }
    

    Like so:

    $ real_url http://dx.doi.org/10.1016/j.pgeola.2020.06.005 
    https://www.sciencedirect.com/science/article/abs/pii/S0016787820300638?via%3Dihub
    

提交回复
热议问题