重定向 curl 后获取最终 URL [英] Get final URL after curl is redirected

查看:42
本文介绍了重定向 curl 后获取最终 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面重定向后最好使用 curl 或 wget 获取最终 URL.

例如http://google.com 可能会重定向到http://www.google.com.

内容很容易获取(例如 curl --max-redirs 10 http://google.com -L),但我只对最终 url 感兴趣(在前一种情况 http://www.google.com).

有没有什么办法可以只使用 Linux 内置工具来做到这一点?(仅限命令行)

解决方案

curl-w 选项 和子变量 url_effective 就是你寻找.

类似的东西

curl -Ls -o/dev/null -w %{url_effective} http://google.com

更多信息

<前>-L 跟随重定向-s 静默模式.不要输出任何东西-o FILE 将输出写入 <file>而不是标准输出-w FORMAT 完成后输出什么

更多

您可能想要添加 -I (那也是一个大写的 i) ,这将使命令不下载任何正文",但它也会使用 HEAD 方法,这不是问题所包含的内容,并且有可能改变服务器可以.有时,即使服务器对 GET 响应良好,它们对 HEAD 的响应也不是很好.

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.

The contents are easy to get(ex. curl --max-redirs 10 http://google.com -L), but I'm only interested in the final url (in the former case http://www.google.com).

Is there any way of doing this by using only Linux built-in tools? (command line only)

解决方案

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 <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.

这篇关于重定向 curl 后获取最终 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆