How to Follow Redirects Using curl?
Introduction
Before we begin talking about how to follow redirects using curl, let's briefly understand – What is curl?
The curl command uses HTTP and HTTPS transport protocols to send and receive data from the server. The request of the cURL is occasionally diverted to another URL address while the data is being transferred to the URL. It implies that the server, upon receiving a request, forwards it to a different server that has the desired data. It is due to a plugin or a page not found issue.
In this tutorial, you will follow redirects using curl. We will also address a few FAQs on how to follow redirects using curl.
Prerequisite
The system has to have curl installed. Use the following instructions in accordance with your distribution to install it on your Linux system:
sudo apt install curl #Debian/Ubuntu-based
sudo yum install curl #CentOS/RHEL-based
sudo pacman -Sy curl #Arch Linux-based
sudo zypper install curl #OpenSUSE
The output demonstrates that the system has the "curl" package installed.
What are the Redirects in HTTP?
When referring to HTTPs from HTTP or vice versa, redirection is used. Two possible redirecting methods are followed by HTTP:
- Permanent Redirects: The requested URL will forward the curl command to the new URL if the requested resource is moved permanently to a different URL. The permanent redirect's HTTP status code is 301.
- Temporary Redirects: When a URL is being maintained, the requested resource may occasionally redirect to another URL. Other reasons exist for temporary redirects, but they last only a short while and are indicated by the HTTP status code 302.
How to Follow Redirect Using the curl Command?
The curl command's "L" option executes after HTTP URL redirects. The curl command's L option uses the following general syntax:
curl -L [URL]
To revoke the command, use the "curl" option, and to follow the URL, use the "L" option. And specify the URL that will be redirected.
curl -L www.vegastack.com
The curl command is used with many options to follow the redirects, which are illustrated with numerous examples.
Example 1: Specify Maximum Redirects
The "L" option of the curl command only redirects the URL 50 times by default. With the "-max-redirs" option, any alternative value can be used to override the default one. Set the maximum number of redirects, for instance, to 60 with the command:
curl -L --max-redirs 60 https://vegastack.com
As can be seen from the output, the redirects have been set to 60.
Example 2: Prevent Conversion of HTTPs
Sometimes, when using the curl command to redirect, the server will change the "Https" request to another request, such as HTTP. As we did here for the 301 status code, it can be avoided by using the "-post" option with the status code. For example, enter https://vegastack.com/
as the URL in the command below:
curl -L --post301 https://vegastack.com/
The output demonstrates that the server fails to convert to HTTP or any other protocol.
Example 3: Enable Infinite Redirects
The "1" value of the "-max-redirs" option can be used to create an endless number of redirects to the domain, like we did above for vegastack.com
:
curl -L --max-redirs 1 https://vegastack.com
It permits limitless reroutes to the website at https://vegastack.com
.
Example 4: Authentication of Redirect
The redirect can have a password set on it to make it safe. The user must give approval before the server can attempt to redirect the requested URL. To accomplish this, issue the following command:
curl -L --location-trusted --user "john:12345" https://vegastack.com
The output indicates that the specified URL has received the password. However, it is not an advised method.
FAQs to Follow Redirects Using curl
How do I follow a redirect to retrieve the final URL using curl
?
To follow a redirect and display the final URL, use the -i
option along with -L
:-curl -i -L <URL>
Can I limit the number of redirects to follow with curl
?
Yes, you can limit the number of redirects using the -m
or --max-redirs
option:-curl -L -m 5 <URL>
How can I follow redirects when making HTTP POST requests with curl
?
To follow redirects for POST requests, use the -L
option as usual:-curl -L -X POST -d "data=value" <URL>
Is it possible to follow redirects while saving the output to a file using curl
?
Yes, you can follow redirects and save the output to a file like this:-curl -L -o output.txt <URL>
How do I suppress the progress meter when following redirects with curl
?
Use the -s
or --silent
option to suppress the progress meter:-curl -L -s <URL>
What if I want to follow redirects with curl
but not show the response body?
You can use the -I
option to follow redirects and display only the headers:-curl -I -L <URL>
Can curl
follow redirects for FTP URLs as well?
Yes, curl
can follow redirects for both HTTP and FTP URLs using the -L
option.
Conclusion
To follow the redirect to the URL, use the curl -L <URL>
command. Users need to make sure that the "curl" command is already installed on the system before using it. Redirecting the desired URL without altering the protocol is advantageous. This tutorial offered an explanation of redirects using the curl command, along with other examples.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them.