How-to

Follow Redirects with curl

curl does not follow redirects unless asked. Add -L and it does, with rules about methods and credentials that matter for APIs and site migrations.

Why does curl stop at a redirect?

By design, curl shows you exactly what the server answered. A 301 or 302 response comes back as is, with a Location header naming the new URL and usually a tiny body. That is useful when you want to inspect redirects, and surprising when you expected the final page. Add -L, long form --location, to follow:

curl -sSL http://example.net/ -o page.html

How do I see each hop?

curl -sIL http://example.net/old-pricing | grep -iE '^(HTTP|location)'

With -I for headers only, -L prints the header block of every response in the chain. Typical output shows HTTP/1.1 301 to HTTPS, then HTTP/2 301 to the new path, then HTTP/2 200. Each extra hop costs a round trip, so a chain longer than two is worth flattening on your own server.

How do redirect codes treat methods?

CodeMeaningcurl with -L after a POST
301Moved permanentlySwitches to GET
302Found, temporarySwitches to GET
303See otherSwitches to GET
307Temporary, keep methodRepeats the POST with its body
308Permanent, keep methodRepeats the POST with its body

The switch to GET on 301 and 302 matches what browsers do. If an API redirects a POST with 301 and expects the body again, add --post301 (or --post302, --post303) to keep it. Better still, have the API answer with 307 or 308.

Avoid combining -X POST with -L: -X forces the method on every hop, so curl sends POST to URLs that expect GET. Let data flags set the method, as described in Send POST requests with curl.

How do I limit or detect loops?

curl -sSL --max-redirs 5 -o /dev/null -w '%{http_code} %{num_redirects} %{url_effective}\n' http://example.net/a

The curl tool gives up after 50 redirects by default with "Maximum (50) redirects followed"; --max-redirs lowers it. The -w variables print the final status, the number of redirects taken and the final URL. A loop usually comes from two rules sending traffic back and forth, such as an HTTP-to-HTTPS redirect in nginx combined with an application that thinks it is on HTTP behind a proxy. Setting X-Forwarded-Proto correctly in the proxy typically fixes it.

What happens to credentials on a redirect?

curl sends -u credentials and custom Authorization headers only to the original host. When a redirect points to another host, they are dropped so a malicious redirect cannot capture them. If you trust the destination, --location-trusted keeps sending them. Cookies set with -b follow the usual cookie rules for each host; see Cookies with curl.

How do I check redirects after a site move?

Feed a list of old URLs and compare final destinations:

while read -r u; do
  curl -s -o /dev/null -L --max-redirs 3 -w "%{http_code} %{num_redirects} $u -> %{url_effective}\n" "$u"
done < old-urls.txt

Every line should end with a 200 and a sensible target, ideally after one redirect. Anything ending in 404, or taking more than two hops, needs a direct rule. This matters for search ranking after moving to a new machine or domain; pairing it with --resolve lets you test the new box before DNS changes, as shown in curl and certificate errors.

How do I write a good redirect on my own server?

server {
    listen 80;
    server_name example.net www.example.net;
    return 301 https://example.net$request_uri;
}

One permanent hop straight to the canonical HTTPS URL, keeping the path and query. Use 308 instead of 301 for endpoints that receive POST requests.

Do redirects count against my traffic?

Barely. A redirect response is a few hundred bytes of headers, so even millions of them use little of the 15 TB monthly outgoing allowance. Their real cost is time: each hop adds a full round trip before the page starts loading, which users far away from the server feel most.

Keep Reading

Send POST requests with curl

POST forms, JSON, files and raw bodies with curl, choose the right content type and read the response status for scripts.

Open

Headers with curl

Show response headers, send or remove request headers, pick single header values and test caching, compression and virtual hosts with curl.

Open

Cookies with curl

Send cookies, save them to a jar, reuse sessions across requests and script a login flow with curl, plus how to read the jar format.

Open

The curl command

A practical tour of curl for server admins: fetching, saving, timing, debugging, retrying, resolving and the flags that make scripts reliable.

Open

FAQ

Quick Answers

Add -L or --location to the command.

For 301, 302 and 303 it follows browser behavior. Use --post301 or --post302, or have the server answer 307 or 308.

Use -w '%{url_effective}' together with -L.

Up to 50 by default with -L; change it with --max-redirs.

curl drops them when the host changes. Use --location-trusted only if you trust the new host.