2010-06-19 163 views

回答

93

curl-w选项和子变量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方法,这不是问题所包含的内容和风险变化服务器在做什么。有时候,即使服务器对GET响应良好,服务器对HEAD的响应也不好。

+4

你应该能够使用“-o的/ dev/null的”,如果你不想要的文件 – 2010-06-20 17:38:29

+0

这是一个很好的选择,我从来不知道卷曲可以做到这一点!它永远不会令我惊讶':-)' – Josh 2010-08-27 22:01:43

+1

这是一个比curl更多的shell功能 – user151841 2012-05-31 18:39:53

-2

您可以使用grep。不会告诉你它在哪里重定向呢?只是grep出来。

2

我不知道如何用curl来做,但libwww-perl安装GET别名。

$ GET -S -d -e http://google.com 
GET http://google.com --> 301 Moved Permanently 
GET http://www.google.com/ --> 302 Found 
GET http://www.google.ca/ --> 200 OK 
Cache-Control: private, max-age=0 
Connection: close 
Date: Sat, 19 Jun 2010 04:11:01 GMT 
Server: gws 
Content-Type: text/html; charset=ISO-8859-1 
Expires: -1 
Client-Date: Sat, 19 Jun 2010 04:11:01 GMT 
Client-Peer: 74.125.155.105:80 
Client-Response-Num: 1 
Set-Cookie: PREF=ID=a1925ca9f8af11b9:TM=1276920661:LM=1276920661:S=ULFrHqOiFDDzDVFB; expires=Mon, 18-Jun-2012 04:11:01 GMT; path=/; domain=.google.ca 
Title: Google 
X-XSS-Protection: 1; mode=block 
4

作为另一种选择:

$ curl -i http://google.com 
HTTP/1.1 301 Moved Permanently 
Location: http://www.google.com/ 
Content-Type: text/html; charset=UTF-8 
Date: Sat, 19 Jun 2010 04:15:10 GMT 
Expires: Mon, 19 Jul 2010 04:15:10 GMT 
Cache-Control: public, max-age=2592000 
Server: gws 
Content-Length: 219 
X-XSS-Protection: 1; mode=block 

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<TITLE>301 Moved</TITLE></HEAD><BODY> 
<H1>301 Moved</H1> 
The document has moved 
<A HREF="http://www.google.com/">here</A>. 
</BODY></HTML> 

但它不走过去的第一个。

3

谢谢。我最终实现你的建议:卷曲-i +用grep

curl -i http://google.com -L | egrep -A 10 '301 Moved Permanently|302 Found' | grep 'Location' | awk -F': ' '{print $2}' | tail -1 

返回空白,如果该网站不重定向,但是这是不够好,我作为它的工作原理在连续重定向。

可能是越野车,但一目了然,它工作正常。

18

谢谢,这帮了我。我做了一些改进,并包裹在一个脚本 “finalurl”:

#!/bin/bash 
curl $1 -s -L -I -o /dev/null -w '%{url_effective}' 
  • -o输出到/dev/null
  • -I实际上不下载,才发现最后的URL
  • -s静音模式,没有progressbars

这使得它可以调用其他脚本命令是这样的:

echo `finalurl http://someurl/` 
+1

感谢那些想法。我重写了它作为一个函数在我的.bashrc文件中的终端使用,并且不需要该文件中的简要选项,所以我使用长名称来自我记录:'finalurl(){curl --silent - location --head --output/dev/null - 写出'%{url_effective}' - “$ @”; }' – buggy3 2017-02-10 18:14:04

5

你可以用wget来做到这一点。 wget --content-disposition“url”此外如果您添加-O /dev/null,您将不会实际保存文件。

wget -O /dev/null --content-disposition example.com

0

这会工作:

curl -I somesite.com | perl -n -e '/^Location: (.*)$/ && print "$1\n"'