卷曲。检查重定向


8

假设我们有3个链接:link1,link2,link3。link1重定向到link2,link2重定向到link3。那么如何看待卷曲呢?

Answers:


13

您可以使用查看HTML标头-I。如果重定向是元刷新,则应该以这种方式作为标头。

lamp@oort ~ $ curl -I http://google.com<br>
HTTP/1.1 301 Moved Permanently<br>
Location: http://www.google.com/<br>
Content-Type: text/html; charset=UTF-8<br>
Date: Thu, 21 Nov 2013 14:59:13 GMT<br>
Expires: Sat, 21 Dec 2013 14:59:13 GMT<br>
Cache-Control: public, max-age=2592000<br>
Server: gws<br>
Content-Length: 219<br>
X-XSS-Protection: 1; mode=block<br>
X-Frame-Options: SAMEORIGIN<br>
Alternate-Protocol: 80:quic

如果重定向是通过PHP进行的,则可以通过比较浏览器的运行位置与实际运行的位置进行比较来检测到这一点。使用Python,JS等进行编码的方法有很多。一个项目可能很有趣给您的是phantomjs,这是一个可编写脚本的无头浏览器。


5

尝试这个 :

for link in link1 link2 link3; do
    curl -Is "$link" | awk '/Location/{print $2}'
done

或使用

for link in link1 link2 link3; do
    printf '%s\n%s\n\n%s\n' 'HEAD / HTTP/1.1' "Host: $link" 'Connexion:close' |
    netcat $link 80 | awk '/Location/{print $2}'
done

4

来自man curl

   -w, --write-out <format>
          Defines what to display on stdout after a completed and
          successful operation.

          <...>

          redirect_url   When an HTTP request was made without -L to
                         follow redirects, this variable will show the 
                         actual URL a redirect would take you to.
                         (Added in 7.18.2)

因此,可能curl -w "%{redirect_url}" link1会给您第一个重定向URL。

也许这样的事情适合您:

URL="http://google.com"
while [ -n "${URL}" ]
do
    echo $URL
    URL=$(curl -sw "\n\n%{redirect_url}" "${URL}" | tail -n 1)
done
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.