1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_FOLLOWLOCATION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLINFO_REDIRECT_COUNT (3)
9  - CURLINFO_REDIRECT_URL (3)
10  - CURLOPT_POSTREDIR (3)
11  - CURLOPT_PROTOCOLS (3)
12  - CURLOPT_REDIR_PROTOCOLS (3)
13---
14
15# NAME
16
17CURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects
18
19# SYNOPSIS
20
21~~~c
22#include <curl/curl.h>
23
24CURLcode curl_easy_setopt(CURL *handle, CURLOPT_FOLLOWLOCATION, long enable);
25~~~
26
27# DESCRIPTION
28
29A long parameter set to 1 tells the library to follow any Location: header
30redirects that an HTTP server sends in a 30x response. The Location: header
31can specify a relative or an absolute URL to follow.
32
33libcurl issues another request for the new URL and follows subsequent new
34Location: redirects all the way until no more such headers are returned or the
35maximum limit is reached. CURLOPT_MAXREDIRS(3) is used to limit the
36number of redirects libcurl follows.
37
38libcurl restricts what protocols it automatically follow redirects to. The
39accepted target protocols are set with CURLOPT_REDIR_PROTOCOLS(3). By
40default libcurl allows HTTP, HTTPS, FTP and FTPS on redirects.
41
42When following a redirect, the specific 30x response code also dictates which
43request method libcurl uses in the subsequent request: For 301, 302 and 303
44responses libcurl switches method from POST to GET unless
45CURLOPT_POSTREDIR(3) instructs libcurl otherwise. All other redirect
46response codes make libcurl use the same method again.
47
48For users who think the existing location following is too naive, too simple
49or just lacks features, it is easy to instead implement your own redirect
50follow logic with the use of curl_easy_getinfo(3)'s
51CURLINFO_REDIRECT_URL(3) option instead of using
52CURLOPT_FOLLOWLOCATION(3).
53
54# NOTE
55
56Since libcurl changes method or not based on the specific HTTP response code,
57setting CURLOPT_CUSTOMREQUEST(3) while following redirects may change
58what libcurl would otherwise do and if not that carefully may even make it
59misbehave since CURLOPT_CUSTOMREQUEST(3) overrides the method libcurl
60would otherwise select internally.
61
62# DEFAULT
63
640, disabled
65
66# PROTOCOLS
67
68HTTP(S)
69
70# EXAMPLE
71
72~~~c
73int main(void)
74{
75  CURL *curl = curl_easy_init();
76  if(curl) {
77    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
78
79    /* example.com is redirected, so we tell libcurl to follow redirection */
80    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
81
82    curl_easy_perform(curl);
83  }
84}
85~~~
86
87# AVAILABILITY
88
89Along with HTTP
90
91# RETURN VALUE
92
93Returns CURLE_OK if HTTP is supported, and CURLE_UNKNOWN_OPTION if not.
94