问题描述
在页面重定向(最好使用curl或wget)之后,我需要获取最终的URL.
I need to get the final URL after a page redirect preferably with curl or wget.
例如, http://google.com 可能会重定向到 http://www.google.com .
For example http://google.com may redirect to http://www.google.com.
内容易于获取(例如curl --max-redirs 10 http://google.com -L
),但我只对最终URL感兴趣(在前一种情况下 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).
仅使用Linux内置工具有没有办法做到这一点? (仅命令行)
Is there any way of doing this by using only Linux built-in tools? (command line only)
推荐答案
curl
的-w
选项,子变量url_effective
寻找.
curl
's -w
option and the sub variable url_effective
is what you arelooking for.
类似
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方法,这不是所包含的问题,并且可能会更改服务器的功能.有时,即使服务器对GET的响应很好,服务器对HEAD的响应也不会很好.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!