Curl follow HTTP redirect

Curl is a command-line tool for transferring data between a local computer and a remote server

2 min read
By myfreax
Curl follow HTTP redirect
Curl follow redirect

Curl is a command-line tool for transferring data between a local computer and a remote server.

When using curl you can download or upload data using protocols such as HTTP, HTTPS, SCP , SFTP and FTP .

The Curl command is pre-installed on most Linux distributions. To check if your Linux distribution has Curl installed, press the shortcut key CTRL+ALT+Tto open Terminal, type curl and press Enter.

If curl is installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information.

Install Curl

Otherwise, the terminal prints the message curl command not found. If you don't have Curl installed, you can install it using your distribution's package manager.

If your computer is running a Debian-based Linux distribution such as Ubuntu, Linux Mint, etc. Please run sudo apt install curl to install Curl.

If your computer is running a Redhat-based Linux distribution, such as CentOS, Fedora, etc. Please run sudo yum install curl to install Curl.

sudo yum install curl
sudo apt install curl

Follow HTTP redirection

If you try to scrape the google.com homepage without wwwthe , you'll notice that Google returns the content of the 301 page, run the command curl google.com.

As you can see from the page content of the 301 redirect, it google.comis redirected to the www version. And by default Curl commands don't follow the HTTP Location header.

So you won't get the source code of the Google homepage. To follow a 301 redirect use the -L/ --locationoption of the Curl command, the Curl command will not terminate until the server does not return a status code of 301.

curl -L google.com
Linux Curl Command Detailed Tutorial | myfreax
curl is a command-line tool for transferring data between a local computer and a remote server

Related Articles