How to follow redirects with curl
Follow HTTP 301 and 302 redirects automatically with curl using the -L flag.
How to follow redirects with curl
Follow HTTP 301 and 302 redirects automatically with curl using the -L flag.
Prerequisites
- curl installed.
Step-by-Step: Follow Redirects with curl
Add the
-L(or--location) flag to make curl follow HTTP redirect responses (301, 302, 303, 307, 308):curl -L https://example.com/old-pageWithout
-L, curl returns the redirect response body instead of following the new URL.Limit the number of redirects to prevent infinite redirect loops:
curl -L --max-redirs 5 https://example.com/old-pageDisplay each redirect step with verbose output:
curl -L -v https://example.com/old-page 2>&1 | grep "< Location:"
How to Verify Redirects Are Followed
curl shows the final URL with
-w:
curl -L -o /dev/null -s -w "Final URL: %{url_effective}\n" https://example.com/old-page