2026.06.03 | youres | 25次围观
前言
在用curl检测网站重定向时,很多人会被url_effective和url_redirect这两个输出变量搞糊涂。它们看起来都能输出URL,但实际行为天差地别。今天用5个实战场景把它们彻底讲清楚。
两个变量的基本区别
url_effective:最终到达的URL地址(经过所有重定向后的最终URL)
url_redirect:最后一个重定向跳转的目标URL(也就是Location头里的值)
简单理解
url_effective= 你最终到达的地方url_redirect= 你经过的最后一个路口
实战对比:5个场景让你彻底搞懂
场景1:没有重定向
# 直接请求无重定向的URL
curl -sI -w "url_effective: %{url_effective}\nurl_redirect: %{url_redirect}\n" https://youres.cn/
# 输出结果
url_effective: https://youres.cn/
url_redirect: (空)
没有重定向时,url_redirect为空,因为根本没有"重定向"这个动作。
场景2:单次301重定向
# 请求会301重定向的URL
curl -sI -w "url_effective: %{url_effective}\nurl_redirect: %{url_redirect}\n" http://example.com/
# 输出结果
url_effective: https://example.com/
url_redirect: https://example.com/
单次重定向时,两者输出相同,都是最终到达的地址。
场景3:多次重定向
# 假设发生3次重定向:A -> B -> C -> D
curl -sI -w "url_effective: %{url_effective}\nurl_redirect: %{url_redirect}\n" https://A.com/
# 输出结果
url_effective: https://D.com/ # 最终到达的地方
url_redirect: https://C.com/ # 最后一个重定向的目标
这里就是关键区别了!
url_effective= D(最终目的地)url_redirect= C(跳向D的那个路口)
场景4:检测短链参数丢失
# 短链通常会重定向到目标URL
curl -L -w "url_effective: %{url_effective}\nurl_redirect: %{url_redirect}\n" "https://short.url?utm_source=test"
# 输出结果
url_effective: https://target.com/page
url_redirect: https://target.com/page # UTM参数可能在这里就丢了
配合-L参数使用,可以看到重定向过程中参数是在哪一跳丢掉的。
场景5:区分UTM参数保留情况
# 对比两个URL的重定向情况
curl -sI -w "url_effective: %{url_effective}\nurl_redirect: %{url_redirect}\n" "https://url?utm_source=google&utm_medium=cpc"
# 正常保留参数
url_effective: https://target.com/?utm_source=google&utm_medium=cpc
url_redirect: https://target.com/?utm_source=google&utm_medium=cpc
# 参数丢失
url_effective: https://target.com/
url_redirect: https://target.com/
当参数丢失时,两者都会反映出来,但你需要配合num_redirects来确认重定向次数。
配合num_redirects使用
curl -sI -w "重定向次数: %{num_redirects}\n最终URL: %{url_effective}\n最后跳转: %{url_redirect}\n" https://short.url?utm_source=test
输出解读
num_redirects= 0:无重定向num_redirects= 1:单次跳转,url_effective == url_redirectnum_redirects>= 2:多次跳转,url_redirect是最后一步,url_effective是最终目的地
实际应用场景
1. 短链巡检脚本
#!/bin/bash
while read url; do
result=$(curl -sI -w "|%{http_code}|%{num_redirects}|%{url_effective}|%{url_redirect}" -o /dev/null "$url")
echo "$url -> $result"
done < urls.txt
2. UTM参数保留检测
check_utm() {
url="$1"
effective=$(curl -sI -w "%{url_effective}" -o /dev/null "$url")
if echo "$effective" | grep -q "utm_"; then
echo "✓ UTM参数保留"
else
echo "✗ UTM参数丢失"
fi
}
3. 重定向链路分析
curl -v -w "\n最终: %{url_effective}\n最后: %{url_redirect}\n次数: %{num_redirects}" https://short.url 2>&1 | grep -E "(Location:|最终:|最后:|次数:)"
总结对比表
| 场景 | url_effective | url_redirect |
|---|---|---|
| 无重定向 | 原始URL | (空) |
| 单次重定向 | 最终URL | 最终URL |
| 多次重定向 | 最终URL | 最后一步URL |
| 检测参数丢失 | ✓ | ✓ |
| 分析跳转链路 | 看终点 | 看最后一步 |
| 判断是否有重定向 | - | 最佳选择 |
结论
两个变量的选择场景:
- 想知道"最终去哪了" → 用
url_effective - 想知道"最后一次跳转往哪跳" → 用
url_redirect - 检测是否有重定向 → 看
url_redirect是否为空 - 检测参数在哪一跳丢失 → 两者结合
num_redirects
最常用的组合是:
curl -sI -w "状态码: %{http_code}\n重定向次数: %{num_redirects}\n最终URL: %{url_effective}\n最后跳转: %{url_redirect}\n" -o /dev/null "目标URL"
这样一次请求就能拿到完整的重定向诊断信息。
相关阅读
版权声明
本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论