Sub topic 8
Tracing a URL with curl
This chapter covers how to use curl to see every hop a URL takes before it reaches a final page. It assumes you can run commands in a terminal, know what a status code and a Location header are, and have read the earlier chapters on status codes and redirect types.
Why trace with curl
A browser hides redirects. You type a URL, a page appears, and the address bar may show something different from what you typed. To fix a redirect chain you need to see each hop, its status code, and its destination. curl shows all of that and is available on almost every server, laptop, and CI runner.
Two rules apply to everything below:
- curl does not follow redirects unless you pass
-L. Without it, you see only the first response. - Test with the same method a crawler would use. A crawler normally sends GET requests, but a HEAD request is cheaper. Some servers answer HEAD differently from GET, so confirm important findings with GET.
Look at one response
Start with a single request and no redirect following. -I sends a HEAD request and prints only the headers. -s hides the progress meter.
curl -sI https://example.com/old-pageA typical result for a moved page looks like this:
HTTP/2 301
location: /new-page
content-type: text/htmlThe status line tells you the server answered 301. The Location header tells you where to go next. Note that Location may be relative (/new-page) or absolute. A client resolves a relative value against the URL it just requested.
Follow the whole chain
Add -L to follow redirects. With -I, curl prints the headers of every hop in order.
curl -sIL https://example.com/old-page | grep -iE '^(HTTP|location)'The grep keeps only the status lines and Location headers. -i makes the match case-insensitive, which matters because HTTP/2 sends header names in lowercase. Example output for a chain with two redirects:
HTTP/2 301
location: http://example.com/old-page/
HTTP/1.1 301
location: https://www.example.com/new-page
HTTP/2 200Read it from top to bottom. Each HTTP line is a response, and the location line under it is the next request. This chain has two redirects before the final 200, so it should be collapsed into one hop.
Summarize with write-out variables
For a quick count rather than a full listing, use -w (write-out). It prints values after the transfer finishes. -o /dev/null discards the page body.
curl -sL -o /dev/null -w '%{num_redirects} redirects, final status %{http_code}, final URL %{url_effective}\n' https://example.com/old-pageOutput:
2 redirects, final status 200, final URL https://www.example.com/new-pageThis form is easy to run over many URLs in a shell loop, and the numbers are easy to compare. Useful variables:
| Variable | Meaning |
|---|---|
| `%{http_code}` | Status code of the last response |
| `%{num_redirects}` | Number of redirects followed |
| `%{url_effective}` | The final URL after redirects |
| `%{redirect_url}` | The `Location` target of the current response, when you do not use `-L` |
| `%{time_total}` | Total time in seconds |
%{redirect_url} is handy without -L: it gives the resolved absolute target, so you do not have to resolve relative Location values yourself.
Read the chain, not just the end
The final status code is only part of the story. Check each hop for these problems.
| What you see | What it means | Usual fix |
|---|---|---|
| Two or more redirects before a 200 | Redirect chain | Point the first URL straight at the final URL |
| `302` on a move that is permanent | Wrong redirect type | Use 301 or 308 |
| Final status 404 or 410 | Redirect leads to a dead page | Fix the target or return 410 at the source |
| Final status 200 on a page that says "not found" | Possible soft 404 | Make the server return 404 |
| `curl: (47) Maximum (50) redirects followed` | Redirect loop | Find the two rules that point at each other |
| Protocol or host changes in `Location` | HTTP to HTTPS or non-www to www hop | Link to the canonical form directly |
Diagnose a loop
A loop never reaches a final page. curl stops after 50 redirects by default and reports error 47. Use --max-redirs to stop earlier so the output stays readable:
curl -sIL --max-redirs 5 https://example.com/loop | grep -iE '^(HTTP|location)'The repeated location values show the cycle. A common cause is a rule that adds a trailing slash while another rule strips it, so /a goes to /a/ and /a/ goes back to /a.
Check what a crawler would see
Servers sometimes respond differently depending on request headers. Three flags help you reproduce what a crawler sees:
-Asets the user agent. Compare the default with a crawler string such asGooglebotto catch rules that treat bots differently.-Hadds a header, for example-H 'Accept-Language: de'to test language-based redirects.-kskips certificate validation. Use it only to debug a certificate problem you already suspect, never to confirm that a site is healthy.
curl -sIL -A 'Googlebot/2.1 (+http://www.google.com/bot.html)' https://example.com/old-page | grep -iE '^(HTTP|location)'A user agent string only changes what the server sees. It does not prove what real Googlebot gets, because some sites verify bots by IP address.
Check many URLs from a file
Put one URL per line in urls.txt and loop over it. The format string prints one line per URL, which you can sort or filter.
while read -r url; do
curl -sL -o /dev/null --max-redirs 10 \
-w '%{http_code} %{num_redirects} %{url_effective}\n' "$url"
done < urls.txtQuoting "$url" matters. Without quotes, characters such as & or ? in a URL can break the command.
To list only the problems, pipe the output through awk:
while read -r url; do
curl -sL -o /dev/null --max-redirs 10 \
-w "%{http_code} %{num_redirects} $url -> %{url_effective}\n" "$url"
done < urls.txt | awk '$1 >= 400 || $2 > 1'This prints every URL that ended in an error or needed more than one redirect. If curl fails before getting a response (DNS failure, timeout), %{http_code} is 000, which the filter also catches.
Gotchas
- HEAD is not always supported. Some servers return 405 or a different status for HEAD. If
-Ilooks wrong, repeat withcurl -s -o /dev/null -D - URL, which sends GET and prints the headers. - Method changes on redirect. With
-L, curl may change POST to GET after a 301, 302, or 303. Codes 307 and 308 preserve the method. This only matters for forms and APIs, but it is a good reason to know the difference. - Cookies and caches. A server may redirect based on a cookie, and browsers cache 301 responses aggressively. If curl and your browser disagree, curl is usually the one reporting the server's real behavior.
- Fragments are never sent. A
#sectionpart of a URL stays on the client, so the server cannot redirect on it. - Authentication and geo rules. A page behind a login or a country rule will show a different chain from a different network.
Practice
Exercise
You run curl -sIL https://example.com/a | grep -iE '^(HTTP|location)' and see: HTTP/2 302, location: /b, HTTP/2 301, location: https://example.com/c, HTTP/2 200. How many redirects are in the chain, and what should you change?
Show answer
There are two redirects (a to b, then b to c) before the final 200. Change the rule for /a to redirect directly to https://example.com/c in one hop. If the move is permanent, use 301 (or 308) instead of the 302 on the first hop.
Exercise
A command prints curl: (47) Maximum (50) redirects followed. Which flag lets you see the cycle without waiting for 50 hops, and what does the output of the trace usually show?
Show answer
Use --max-redirs with a small number such as 5 together with -sIL. The repeated location lines show two or more URLs pointing at each other, for example /a redirecting to /a/ and /a/ redirecting back to /a.