乐闻世界logo
搜索文章和话题

How does cURL handle HTTP redirects?

3月6日 23:08

Redirect handling is a common requirement when accessing web resources with cURL. Properly handling redirects ensures requests reach the final destination.

Basic Redirect Handling

bash
# Don't follow redirects (default behavior) curl http://example.com # Automatically follow redirects curl -L http://example.com curl --location http://example.com # Show redirect process curl -L -v http://example.com

Redirect Limit

bash
# Limit maximum redirects (default 50) curl -L --max-redirs 5 http://example.com # Unlimited redirects (not recommended) curl -L --max-redirs -1 http://example.com

POST Request Redirect Behavior

bash
# Default: POST becomes GET after redirect curl -L -X POST -d "data=test" http://example.com/submit # Keep POST method (RFC 7231) curl -L --post301 -X POST -d "data=test" http://example.com/submit # Keep POST method (non-standard) curl -L --post302 -X POST -d "data=test" http://example.com/submit curl -L --post303 -X POST -d "data=test" http://example.com/submit

Redirect Types

Status CodeTypeDefault Behavior
301Permanent RedirectPOST → GET
302Temporary RedirectPOST → GET
303See OtherPOST → GET
307Temporary RedirectKeep Method
308Permanent RedirectKeep Method

View Redirect Chain

bash
# Show all redirect URLs curl -L -v http://example.com 2>&1 | grep -E "(< HTTP|< Location)" # Output final URL using -w curl -L -w "Final URL: %{url_effective}\n" -o /dev/null -s http://example.com # Get redirect count curl -L -w "Redirects: %{num_redirects}\n" -o /dev/null -s http://example.com

Common Redirect Scenarios

bash
# HTTP to HTTPS redirect curl -L http://example.com # www to non-www redirect curl -L http://www.example.com # Short link expansion curl -L -w "Final URL: %{url_effective}\n" -o /dev/null -s https://bit.ly/shorturl # Redirect after login curl -L -c cookies.txt -b cookies.txt \ -X POST \ -d "username=admin&password=123456" \ http://example.com/login

Advanced Redirect Control

bash
# Limit redirect protocols curl -L --proto-redir =https http://example.com # Keep headers during redirect curl -L --location-trusted \ -H "Authorization: Bearer token123" \ http://example.com/api # View cookies before and after redirect curl -L -c cookies.txt -b cookies.txt -v http://example.com 2>&1 | grep -i cookie # Modify headers during redirect curl -L -H "Host: newdomain.com" http://example.com

Redirect Debug Script

bash
#!/bin/bash # Redirect trace script URL="http://example.com" echo "========== Redirect Chain Trace ==========" echo "Initial URL: $URL" echo "" # Method 1: Use -v to view curl -L -v "$URL" 2>&1 | grep -E "(< HTTP|< Location)" | head -20 echo "" echo "========== Final Info ==========" # Get final URL FINAL_URL=$(curl -L -w "%{url_effective}" -o /dev/null -s "$URL") echo "Final URL: $FINAL_URL" # Get redirect count REDIRECTS=$(curl -L -w "%{num_redirects}" -o /dev/null -s "$URL") echo "Redirect Count: $REDIRECTS" # Get final status code STATUS=$(curl -L -w "%{http_code}" -o /dev/null -s "$URL") echo "Final Status Code: $STATUS"

Common Issues

bash
# Issue 1: Redirect loop # Solution: Limit redirect count curl -L --max-redirs 10 http://example.com # Issue 2: Lost authentication on cross-domain redirect # Solution: Use --location-trusted curl -L --location-trusted -H "Authorization: Bearer token" http://example.com # Issue 3: POST data lost # Solution: Use --post302 or --post301 curl -L --post302 -X POST -d "data=test" http://example.com # Issue 4: HTTPS redirect fails # Solution: Ensure SSL certificate is valid or use -k curl -L -k http://example.com # Issue 5: Redirect to different domain # Solution: Check and update Host header curl -L -H "Host: newdomain.com" http://example.com

Practical Examples

bash
# Complete redirect-aware API call curl -L --max-redirs 5 \ --post302 \ -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"action":"submit"}' \ -w "\nFinal URL: %{url_effective}\nRedirects: %{num_redirects}\nHTTP Status: %{http_code}\n" \ http://api.example.com/submit # Batch short link expansion for url in "bit.ly/abc" "t.co/xyz"; do echo -n "$url -> " curl -L -w "%{url_effective}" -o /dev/null -s "https://$url" echo "" done

标签:cURL