且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重定向 curl 后获取最终 URL

更新时间:2023-11-26 16:41:10

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.