Sub topic 8
Tracing a URL with curl
This chapter assumes you can use a command line, know what a status code is, and know the difference between a 301 and a 302 redirect. curl is a command-line tool that sends HTTP requests and shows you the responses. It works like a crawler, so you see what a crawler sees and not what your browser's cache and cookies show you.
Why trace by hand
A browser follows redirects silently and caches permanent ones. A trace shows every hop, its status code, and where it points. You need this to confirm a fix, to explain a crawler report, or to find where a chain or loop begins.
Show the redirect chain
Send a HEAD request, follow redirects, and keep only the lines that matter:
curl -sIL https://example.com/old-page | grep -iE '^(HTTP|location)'The flags do the following:
-shides the progress meter.-Isends a HEAD request, so only headers come back.-Lfollows redirects.
The output has one HTTP line for each hop and a location line for each redirect:
HTTP/2 301
location: https://example.com/old-page/
HTTP/2 301
location: https://example.com/new-page
HTTP/2 200This path has two redirects before the final 200. That is a chain. The first hop should point straight to /new-page.
Summarize the result
For a quick pass or a script, ask curl for a summary with write-out variables:
curl -sL -o /dev/null -w '%{num_redirects} redirects, final status %{http_code}, final URL %{url_effective}\n' https://example.com/old-page-o /dev/null discards the body. -w prints the variables after the transfer finishes. Look for these results:
| Result | Meaning |
|---|---|
| 0 redirects, status 200 | The URL works as it is |
| 1 redirect, status 200 | One clean hop |
| 2 or more redirects | A chain to collapse |
| Final status 404 or 410 | A redirect that ends on a missing page |
| curl error "Maximum (50) redirects followed" | A loop |
Check one hop at a time
Drop -L to see a single response. This is useful when you suspect a loop or want to read a Location header exactly:
curl -sI https://example.com/old-pageA Location value can be relative, such as /new-page. Resolve it against the current URL before you follow it.
Common mistakes
- Testing the wrong variant.
http://,https://, with and withoutwww, and with and without a trailing slash can each behave differently. Trace the exact URL that appears in your links. - Trusting the browser. A browser may show a cached 301 that the server no longer sends. Use curl to see the live answer.
- Reading only the last status. A final 200 can hide three redirects. Always check the hop count.
- Ignoring user agents. Some servers treat bots differently. Add
-A "Googlebot"to see whether the response changes. This only shows how the server treats that header, not how Google's real crawler is treated.
Exercise
You run curl -sL -o /dev/null -w '%{num_redirects} %{http_code}\n' https://example.com/a and it prints 3 200. Is the URL healthy, and what do you do next?
Show answer
The page loads, but three redirects mean a chain. Run curl -sIL with the grep filter to see each hop, then change the first redirect (and any internal links) so it points straight to the final URL in one hop.
Exercise
A trace ends with "curl: (47) Maximum (50) redirects followed". What is the likely cause and how do you find the point where it starts?
Show answer
A redirect loop, where URLs point back to each other. Run curl -sI without -L on the URL and follow each Location value by hand until a URL repeats. That repeated URL shows which rules conflict.