Checking Redirect Paths and Final URLs with curl: Tracking 301 and 302

ネットワーク・RFCカテゴリを表すパンダのイラスト Networking / RFC

About This Article
This article was created using an automated generation workflow leveraging generative AI. Based on RFC 9110 and official curl documentation, it organizes how to check redirects such as 301 and 302 by separating them into the initial response, intermediate path, and final URL.

Verification Status: 📘 Confirmed via RFC and official curl documentation, execution not verified.

Because browsers automatically follow redirects, it can sometimes be difficult to see whether the initial connection was HTTP, whether it was forwarded to a www version, or how many redirect steps occurred.

With curl, you can:Separately inspect the pre-transfer Location and the final destination URL.

First, Check the Initial Response

curl -I https://example.com/

When status codes like 301, 302, 307, or 308 are returned, check Location.

HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/

At this point, curl will -Inot automatically proceed to the next URL if used alone.

Following Redirects with -L

curl -I -L https://example.com/

-L / --locationAdding

will follow the Location header. If there are multiple steps, the response headers will be displayed in sequence.

sequenceDiagram
  participant C as curl
  participant A as URL A
  participant B as URL B
  participant C2 as URL C
  C->>A: Request
  A-->>C: 301 Location: B
  C->>B: Request
  B-->>C: 302 Location: C
  C->>C2: Request
  C2-->>C: 200 OK

Finding Only the Final URL

You can display the final URL by using curl's write-out feature.

curl -sS -L -o /dev/null   -w 'HTTP=%{http_code}nFINAL=%{url_effective}n'   https://example.com/

The following two points should be checked:

  • http_code: Status of the final response

  • url_effective: Finally reached URL

Use Cases

In practice, this is useful for checking the following:

  • HTTP to HTTPS forwarding

  • Enforcing www/non-www consistency

  • 301 redirects from old URLs to new URLs

  • WordPress canonical URL changes

  • Reverse proxy configurations

  • Forwarding behavior after implementing a CDN

  • Pre-authentication URL to login URL transitions

In SEO and site migrations, not only whether a redirection occurs, but also how many hops are involvedis also important. Unnecessary multi-stage redirects can serve as a clue for architectural reviews.

Isolating Infinite Loops

With configuration errors such as A -> B -> A, browsers may simply display an error saying "Too many redirects."

With curl, you can explicitly specify the maximum number of redirects.

curl -I -L --max-redirs 5 https://example.com/

If it exceeds five attempts, an error occurs, allowing you to suspect a loop or excessive forwarding.

Caution with URLs Containing Credentials

If the redirect destination is a different host, handle credentials and cookies with careful verification.

In particular, --location-trustedmay send credentials to a different host, so it is safer not to use it without understanding its implications.

When sharing troubleshooting results, make sure not to paste

  • Cookies

  • Authorization headers

  • Internal URLs

  • Session information

as-is.

Going Beyond the Difference Between 301 and 302

What is required in the field is not just memorizing status codes, but confirming:

  1. Where the initial response was directed

  2. Where it went next

  3. Where the final URL is

  4. What the final status code is

  5. Whether the intended path was followed

.

Summary

  • -IYou can check the initial Location with

  • -LYou can track redirects with

  • %{url_effective}You can check the final URL with

  • --max-redirsCan be used for investigating redirect loops

  • Handle redirects involving credentials with caution

Official Documentation and Primary Sources

Document information

Article title
Checking Redirect Paths and Final URLs with curl: Tracking 301 and 302
Published
Updated
Source
https://papanda925.com/?p=15382&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL