72

I can download a file by url but when I try it from bash I get a html page instead of a file.

How to download file with url redirection (301 Moved Permanently) using curl, wget or something else?

UPD

Headers from the url request.

curl -I http://www.somesite.com/data/file/file.rar

HTTP/1.1 301 Moved Permanently
Date: Sat, 07 Dec 2013 10:15:28 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3
Location: http://www.somesite.com/files/html/archive.html
Vary: Accept-Encoding
Content-Type: text/html
X-Pad: avoid browser bug
edem
  • 3,222
  • 3
  • 19
  • 45

1 Answers1

102

Use -L, --location to follow redirects:

$ curl -L http://httpbin.org/redirect/1
deltheil
  • 15,496
  • 2
  • 44
  • 64
  • Get the content of the page from Location header. I've updated the question. – edem Dec 07 '13 at 13:21
  • Hmm... but if you try to download a file (a binary archive here) and the `Location` header redirects you to a HTML file then the archive is no more available... I assume this HTML page contains an error message, isn't it? If not, does it include information (i.e. link) related to the original file you're trying to download? In such a case you would have to parse the HTML to extract it. – deltheil Dec 07 '13 at 13:34
  • No, HTML page doesn't contain error info but it contain relative path to file: ` – edem Dec 07 '13 at 13:45
  • 1
    So you need to first resolve this issue, then parse and reconstruct the right link to follow this redirection. – deltheil Dec 09 '13 at 13:34
  • 2
    Works for me. Example, I'm downloading the jenkins war file which redirects, I could do `curl -O -L http://mirrors.jenkins.io/war-stable/2.60.1/jenkins.war` – icasimpan Jul 01 '17 at 07:55