The curl command transfers the data to and from the server using the transport protocols like HTTP and HTTPS. Sometimes during the data transfer to the URL, the request of the cURL is redirected to another URL address. It means that when the requests are made to the server, the server redirects the request to another server with the requested information. It is because of a plugin or page not found issue.
This blog will explain the methods of following the redirect while requesting the server using the curl command.
- Install the curl Command in Linux
- What are the Redirects in HTTP?
- How to Follow Redirect Using the curl Command?
- Specify Maximum Redirects
- Prevent Conversion of HTTP
- Enable Infinite Redirects
Prerequisite: Install the curl Command in Linux
The curl must be installed on the system. To install it on your Linux, use the following commands as per your distribution:
$ 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 shows that the “curl” package has been installed in the system.
What are the Redirects in HTTP?
The redirection refers to HTTPs from the HTTP or vice versa. The two types of redirecting can be followed by HTTP:
- Permanent Redirects: When the requested resource is permanently shifted to another URL, the requested URL will redirect the curl command to the new URL. In HTTP, the status code for the permanent redirect is 301.
- Temporary Redirects: Sometimes, the requested resource redirects to some other URL because the maintenance is going on to the URL. There are some other reasons for temporary redirects, but the temporary redirects are for a small time, and the status code for them is 302 in HTTP.
How to Follow Redirect Using the curl Command?
The “L” option of the curl command follows the URL redirects in HTTP. The general syntax of using the L option of the curl command:
$ curl -L [URL]
Use the “curl” to revoke the command and the “L” option to follow the URL. And specify the URL whose redirection is to be followed. For example, we will follow the redirects of our domain:
$ curl -L www.itslinuxfoss.com
The curl command is used with different options to follow the redirects, which are explained with some examples.
Example 1: Specify Maximum Redirects
By default, the “L” option of the curl command redirects the URL only 50 times. This default number can be changed by specifying any other number with the “–max-redirs” option. For example, set the maximum redirects to 60 using the command:
$ curl -L --max-redirs 60 https://itslinuxfoss.com
The output shows that the redirects have been set to 60.
Example 2: Prevent Conversion of HTTPs
Sometimes, while redirecting with the curl command, the server converts the “Https” to some other request, i.e., HTTP. It can be prevented by using the “–post” option with the status code as we did here for the 301 status code. For instance, specify the URL “https://itslinuxfoss.com/” in the following command:
$ curl -L --post301 https://itslinuxfoss.com/
The output shows that the server does not convert to the HTTP or any other protocol.
Example 3: Enable Infinite Redirects
The “–max-redirs” option’s value “1” can be used to make the infinite redirects to the domain, as we did here for “itslinuxfoss.com”:
$ curl -L --max-redirs 1 https://itslinuxfoss.com
It enables the infinite redirects to the domain “https://itslinuxfoss.com”.
Example 4: Authentication of Redirect
To make the redirect secure, the password can be set on it. When the server tries to redirect the requested URL, it has to ask permission from the user. This can be done by following the command:
$ curl -L --location-trusted --user "john:12345" https://itslinuxfoss.com
The output shows that the password has been sent to the requested URL. However, it is not a recommended method.
Conclusion
To follow the redirect to the URL, execute the “curl -L <URL>” command. Before it, users must ensure that the “curl” command is installed in the system. It is beneficial to redirect the targeted URL without changing the protocol. This blog has explained the redirects using the curl command with the help of different examples.