3xx 리다이렉트 응답의 `Location` 헤더를 따라 최종 목적지까지 이동하는 방법과, 반대로 리다이렉트를 따르지 않고 관찰하는 방법을 다룹니다.
3xx 리다이렉트 응답의 `Location` 헤더를 따라 최종 목적지까지 이동하는 방법과, 반대로 리다이렉트를 따르지 않고 관찰하는 방법을 다룹니다.
# follow up to 10 redirects and show the final response:
curl -L --max-redirs 10 https://example.com/old-path
# see the redirect chain without following bodies:
curl -sIL https://example.com/old-path// fetch follows redirects by default (redirect: 'follow').
const res = await fetch('https://example.com/old-path');
console.log(res.redirected, res.url); // true, final URL
// to inspect instead of follow:
const raw = await fetch('https://example.com/old-path', { redirect: 'manual' });import requests
res = requests.get('https://example.com/old-path') # follows by default
print(res.url) # final URL
print([r.status_code for r in res.history]) # the redirect chain
# disable: requests.get(url, allow_redirects=False)curl은 기본적으로 리다이렉트를 따르지 않으므로 `-L`을 명시해야 하고, 무한 루프를 막으려면 `--max-redirs`로 최대 횟수를 제한합니다. 반대로 `fetch`와 `requests`는 기본으로 따라가므로, 리다이렉트 자체를 관찰하려면 `redirect: 'manual'` 또는 `allow_redirects=False`로 끕니다.
상태 코드마다 메서드 보존 규칙이 다릅니다. 301·302·303은 POST를 GET으로 바꾸는 경우가 많고, 307·308은 원래 메서드와 본문을 그대로 유지합니다. 따라서 POST 요청을 리다이렉트할 때 어떤 코드인지에 따라 결과가 크게 달라집니다.
리다이렉트를 따라갈 때 보안에 주의하세요. 다른 출처(origin)로 넘어가면 curl은 기본적으로 `Authorization` 헤더를 떨어뜨립니다. HTTP→HTTPS로 업그레이드하는 리다이렉트는 안전하지만, 신뢰할 수 없는 서버의 `Location`을 무조건 따라가면 SSRF·자격증명 유출 위험이 있습니다.