How To Download Files From URLs With Redirects

It’s a very common task to need to download a file on the command line of a remote server. This is fine when the URL is a direct link to a file. However, often things get annoying and the download link has some PHP that redirects to the actual location of the file e.g.:

https://www.process-one.net/downloads/downloads-action.php?file=/20.12/ejabberd_20.12-0_amd64.deb

Fortunately, cURL can help you here. All you need to do is use it as follows:

curl -L <URL> -o <OUTFILE>

Using the example URL gives us:

curl -L https://www.process-one.net/downloads/downloads-action.php?file=/20.12/ejabberd_20.12-0_amd64.deb -o ejabberd_20.12-0_amd64.deb

Alternatively, if you just want to get direct location of the file use curl -I <URL> e.g.:

$ curl -I https://www.process-one.net/downloads/downloads-action.php?file=/20.12/ejabberd_20.12-0_amd64.deb
HTTP/2 302
server: nginx
date: Mon, 28 Dec 2020 16:22:02 GMT
content-type: text/html; charset=UTF-8
content-length: 82
location: https://static.process-one.net/ejabberd/downloads/20.12/ejabberd_20.12-0_amd64.deb
x-powered-by: WP Engine
x-cacheable: non200
cache-control: max-age=600, must-revalidate
x-cache: HIT: 15
x-cache-group: normal

The location: line will give you the direct download link to the file.