我需要在页面重定向后获取最终 URL,最好使用 curl 或 wget。
例如 http://google.com 可能会重定向到 http://www.google.com .
内容很容易获得(例如 curl --max-redirs 10 http://google.com -L
),但我只对最终 url 感兴趣(在前案例 http://www.google.com )。
有没有办法只使用 Linux 内置工具来做到这一点? (仅限命令行)
最佳答案
curl
的-w
option而子变量 url_effective
就是你
正在寻找。
类似
curl -Ls -o /dev/null -w %{url_effective} http://google.com
更多信息
-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
更多
您可能想添加 -I
(即大写的 i
),这将使命令不下载任何“正文”,但它也使用 HEAD 方法,这不是问题包含的内容,并且可能会更改服务器可以。有时服务器对 HEAD 的响应不佳,即使它们对 GET 响应良好。
https://stackoverflow.com/questions/3074288/