The command
curl -sIL http://example.com/Blog
What each flag does, from the curl manual:
-I(--head) fetches the headers only, using an HTTP HEAD request.-L(--location) follows aLocation:header on a 3xx response and, used with-I, prints the headers of every page it requests. Without-Lyou see only the first hop. curl follows up to 50 redirects by default.-shides the progress meter.
One caveat. Some servers answer HEAD differently from GET, for example with a 405 or 404, or with different headers. If the result looks wrong, repeat with a real GET that prints only the headers: curl -sL -D - -o /dev/null URL.
Worked example: four responses for one link
An old link points to http://example.com/Blog. curl prints this (other headers removed for space):
HTTP/1.1 301 Moved Permanently
Location: https://example.com/Blog
HTTP/2 301
location: https://www.example.com/Blog
HTTP/2 302
location: /blog/
HTTP/2 200
content-type: text/html; charset=utf-8
Each block that starts with HTTP/ is one response. Pasted into the HTTP header checker with the start URL, it becomes this chain:
| # | URL requested | Status | Goes to |
|---|---|---|---|
| 1 | http://example.com/Blog | 301 Moved Permanently | https://example.com/Blog |
| 2 | https://example.com/Blog | 301 Moved Permanently | https://www.example.com/Blog |
| 3 | https://www.example.com/Blog | 302 Found | https://www.example.com/blog/ |
| 4 | https://www.example.com/blog/ | 200 OK | |
and these findings:
- Warning: 3 redirects before the final response. Point the first URL straight at the final one; Googlebot follows at most 10 hops and each hop costs time.
- Warning: The chain contains a temporary redirect (302 or 307). If the move is permanent, use 301 or 308.
- OK: Final response is 200 OK.
Three hops come from three separate rules, each written on its own: one sends HTTP to HTTPS, one adds www, and one fixes the path. Each is correct alone; together they make a chain. And the last hop is a 302, which tells Google the move is temporary.
301 vs 302, 307 and 308: what Google does
| Status | Meaning | |
|---|---|---|
| 301 Moved Permanently | Permanent | Uses the redirect as a strong signal that the target should be canonical; shows the new URL in results. |
| 308 Permanent Redirect | Permanent, keeps the request method | Treated the same as 301. |
| 302 Found | Temporary | A weak signal. Shows the source URL in results; the target may still be indexed if other signals point to it. |
| 307 Temporary Redirect | Temporary, keeps the request method | Treated the same as 302. |
| 303 See Other | "Go and GET this other URL" | Listed by Google with the temporary redirects. |
So the choice is about which URL you want in search results: a permanent redirect for the new one, a temporary redirect to keep the old one. Use a temporary redirect only when the old URL really will come back, for example while a page is briefly replaced by a notice. Google also says that, although it treats 301 and 308 alike (and 302 and 307 alike), you should use the code that fits, because other clients may handle them differently.
Google's crawlers follow up to 10 redirect hops by default. Long chains are rare in practice; the reason to shorten them is speed and clarity, since every hop is another round trip for every visitor.
Where unintended 302s come from
Most temporary redirects on a site were never chosen. They are the default of the tool that made them:
| Written as | Status sent | Permanent version |
|---|---|---|
PHP: header("Location: https://..."); | 302, unless a 201 or 3xx status was already set | header("Location: https://...", true, 301); |
Apache: Redirect "/old" "https://..." | 302 when no status is given | Redirect permanent "/old" "https://..." (or 301) |
nginx: return https://...; | 302 when only a URL is given | return 301 https://...; |
nginx: rewrite ... redirect; | 302 | rewrite ... permanent; |
If curl shows a 302 you did not mean, look for a line like these in the configuration or code that serves that URL.
The fix
Send every old variant straight to the final URL in one permanent redirect. For the example, the redirect for http://example.com/Blog should go directly to https://www.example.com/blog/:
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/blog/
HTTP/2 200
content-type: text/html; charset=utf-8
The header checker's findings for this version:
- OK: One permanent redirect: the clean way to move a URL.
- OK: Final response is 200 OK.
In practice that means one rule that builds the final URL (scheme, host and path together) instead of several rules that each fix one part, or at least ordering the rules so the path fix happens in the same response as the scheme and host fix. Then:
- Update internal links, canonical tags and sitemaps to the final URL, so that your own pages do not go through the redirect at all. Google's sitemap help asks you to replace redirecting URLs in sitemaps with the URLs that should be crawled.
- Run
curl -sILagain on each old variant:http://andhttps://, with and withoutwww, with and without the trailing slash. - Keep the redirects in place. Google's site-move guidance says to keep them for as long as possible, and at least a year.
Two things curl does not show
- JavaScript and meta refresh redirects. curl does not run JavaScript or read HTML, so a page that redirects that way shows as a 200. Google can follow both, but recommends server-side redirects where possible.
- Upgrades the browser does by itself. Once a browser has received a
Strict-Transport-Securityheader from a site, laterhttp://loads of that site "use HTTPS immediately, without requiring a redirect" (MDN). Testing anhttp://URL in that browser therefore tells you nothing about the server's own redirect. curl keeps no such memory unless you give it a cache file with--hsts, so it shows what the server sends.
Sources
- Google: Redirects and Google Search
- Google: How HTTP status codes affect Google's crawlers (10 hops, 301/302/307/308)
- curl manual: -I, -L, --max-redirs, -D
- PHP manual: header() and the Location header
- Apache: mod_alias Redirect
- nginx: ngx_http_rewrite_module (return, rewrite)
- Search Console Help: Sitemaps report (redirects in sitemaps)
- MDN: Strict-Transport-Security
- Google: Site moves with URL changes (how long to keep redirects)