Skip to content
Urlzee

How to Test Redirects with curl

By Urlzee Team · Published · 6 min read

To test a redirect with curl, request the URL without following it and read the headers: curl -s -o /dev/null -D - https://example.com/old. The status line shows the code, such as 301 or 302, and the location header shows where it points. Add -L to follow the whole chain.

How do you see a single redirect?

Ask for the headers and nothing else. By default curl does not follow redirects, which is exactly what you want for testing: you see what the first server said.

curl -s -o /dev/null -D - https://example.com/old-page

The options:

OptionWhat it does
-sSilent: hides the progress meter
-o /dev/nullThrows away the response body
-D -Dumps the response headers to standard output

Typical output looks like this:

HTTP/2 301
location: https://example.com/new-page
content-type: text/html

You will often see curl -I used for the same purpose. -I sends a HEAD request, which asks for headers only. It is shorter to type, but some servers handle HEAD differently from GET, or refuse it. The command above makes a normal GET, as a browser would, so the result is more representative. On Windows, replace /dev/null with NUL.

How do you follow a redirect chain?

Add -L. curl then requests each Location in turn and, with -D -, prints the headers of every response along the way.

curl -s -L -o /dev/null -D - http://example.com/old-page

To cut that down to the essentials, filter for status lines and locations:

curl -s -L -o /dev/null -D - http://example.com/old-page | grep -i -E '^(HTTP|location)'
HTTP/1.1 301 Moved Permanently
Location: https://example.com/old-page
HTTP/2 301
location: https://www.example.com/old-page
HTTP/2 200

That is a two-hop chain: one redirect to HTTPS, one to www, then the page. Why chains like this are worth flattening is explained in redirect chains.

How do you print just the facts you need?

Use -w (write-out), which prints named variables after the transfer finishes. It is the tidiest way to get a single value into a script.

Status code of the first response, and where it redirects:

curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' https://example.com/old-page
301 -> https://example.com/new-page

Final status, number of redirects and final URL after following the chain:

curl -s -L -o /dev/null -w '%{http_code} after %{num_redirects} redirect(s): %{url_effective}\n' https://example.com/old-page

The most useful variables for redirect work:

VariableMeaning
%{http_code}Status code of the last response. 000 means no response was received.
%{redirect_url}The absolute URL a redirect points to, when you did not use -L
%{url_effective}The last URL requested, which is the final destination with -L
%{num_redirects}How many redirects were followed
%{time_redirect}Seconds spent on redirect hops before the final request began
%{time_total}Total seconds for the whole transfer

%{redirect_url} is handy because it resolves relative Location values for you. A server may answer Location: /new-page, and curl reports the full address.

How do you measure how long redirects take?

Compare time_redirect with time_total:

curl -s -L -o /dev/null \
  -w 'redirects: %{num_redirects}\nredirect time: %{time_redirect}s\ntotal time: %{time_total}s\n' \
  http://example.com/old-page

time_redirect covers everything from the first request up to the start of the final one, including DNS lookups and TLS handshakes for each hop. If it is a large share of the total, the chain is costing your visitors real time.

Run it a few times. The first run includes DNS lookups that later runs may get from a cache, and network conditions vary.

How do you catch redirect loops?

Limit the number of hops and check curl's exit status. With -L, curl follows up to 50 redirects by default. --max-redirs lowers that:

curl -s -L --max-redirs 5 -o /dev/null -w '%{http_code} %{url_effective}\n' https://example.com/looping-page
echo "exit status: $?"

When the limit is exceeded, curl stops and exits with status 47. In a script, a non-zero exit status is your signal that the URL never settled. A loop and an over-long chain look the same from here; print the headers with -D - to see which it is.

How do you test what a browser or bot would get?

Send the same headers they would. Servers sometimes redirect differently depending on the client.

Set a user agent with -A:

curl -s -o /dev/null -D - -A 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)' https://example.com/

This is how you check device-based redirects, such as a site that sends phones to a mobile subdomain. Other headers go in with -H:

curl -s -o /dev/null -D - -H 'Accept-Language: fr' https://example.com/

Two things that affect redirects and are easy to forget:

  • Cookies. Some sites redirect until a cookie is set. Add -c cookies.txt -b cookies.txt so curl stores and returns cookies across hops.
  • Query strings. Always quote the URL. An unquoted & is interpreted by your shell, which silently cuts the URL short:
curl -s -L -o /dev/null -w '%{url_effective}\n' 'https://example.com/landing?utm_source=newsletter&utm_medium=email'

That last command is also the quickest way to confirm that UTM parameters survive a redirect: the final URL should still contain them.

How do redirects behave with POST requests?

It depends on the status code. When you send data with -d and follow redirects with -L, curl switches to GET after a 301, 302 or 303, as browsers do, and re-sends the POST after a 307 or 308.

curl -s -L -o /dev/null -w '%{http_code}\n' -d 'name=test' https://example.com/form

Do not add -X POST to a command like this. -X forces the method on every request in the chain, including the ones after a redirect, which is rarely what you want and hides the real behaviour. -d already makes the first request a POST. The method rules behind each code are in 301 vs 302 redirects.

How do you check a list of URLs?

Loop over a file. Put one URL per line in urls.txt and run:

while read -r url; do
  curl -s -L --max-redirs 10 -o /dev/null \
    -w '%{http_code}\t%{num_redirects}\t%{url_effective}\n' "$url"
done < urls.txt

The output is tab-separated, with status code, redirect count and final URL, ready to paste into a spreadsheet. Sort by the second column to find the longest chains, and filter the first for anything that is not 200.

Add --max-time 10 if some hosts may hang, so one dead server does not stall the whole run.

The same way. A short link is a single redirect, so the first response tells you everything:

curl -s -o /dev/null -D - https://urlzee.com/launch

For a Urlzee link, the response includes headers like these:

HTTP/2 302
location: https://example.com/launch
cache-control: private, no-store
x-robots-tag: noindex, nofollow

The 302 and no-store mean every visit goes through the server and nothing caches the redirect, which is how clicks are counted and edited destinations take effect at once. A link that does not exist returns 404, and one that has been disabled or has expired returns 410.

Urlzee recognises curl's default user agent as an automated client, so these test requests are recorded as bot traffic and kept out of your click totals. If you override the user agent with -A to imitate a browser, the request is counted as a click.

To inspect a short link from another service before opening it, the same command shows its destination without your browser ever loading the page. More approaches are in how to check where a short link goes.

When is curl not the right tool?

When the redirect does not happen in HTTP headers. curl does not run JavaScript or act on <meta http-equiv="refresh"> tags, so a page that redirects that way will look like a plain 200. Sites that challenge automated clients may also answer curl with a 403 while working normally in a browser.

For a quick check without a terminal, the link checker and URL expander show each hop, status code and final URL from a web page. For every option mentioned here and many more, the reference is the curl man page.

Frequently asked questions

How do I see where a URL redirects with curl?
Run curl -s -o /dev/null -D - followed by the URL. curl prints the response headers without following the redirect, so the status line shows the code and the location header shows the target.
How do I make curl follow redirects?
Add the -L option, also written --location. curl then requests each Location in turn until it reaches a response that is not a redirect, up to a default limit of 50 hops.
How do I get only the HTTP status code from curl?
Use curl -s -o /dev/null -w '%{http_code}\n' with the URL. The -w option prints the variables you ask for after the transfer, and -o /dev/null hides the body.
Why does curl -I give a different result from my browser?
The -I option sends a HEAD request, and some servers answer HEAD differently from GET or reject it. Servers may also vary responses by user agent. Test with a GET request, and set a browser-like user agent with -A if needed.