curl
Follow Redirects
Chase 3xx responses automatically.
By EZ4Code Team
redirect3xx
Code
# Follow redirects
curl -L https://example.com/redirect
# Limit the number of redirects
curl -L --max-redirs 5 https://example.com
# Show the redirect chain
curl -L -v https://example.com 2>&1 | grep "Location:"
# Preserve POST method on 301/302 (off by default)
curl -L --post301 --post302 -d "x=1" https://example.com
# Stop if a redirect loop is detected
curl -L --max-redirs 3 https://example.comExplanation
The -L flag tells curl to follow Location headers on 301, 302, and 307 responses until a final response is received. By default curl reverts to GET after a 301 or 302, but --post301 and --post302 keep the original POST method. --max-redirs caps the chain to prevent infinite loops.