0

Cloudflare Transform Rules 保留查询参数配置教程:3种规则让HTTPS跳转UTM参数不再丢失

2026.06.01 | youres | 22次围观

问题现象:Cloudflare HTTPS跳转后UTM参数消失了

很多网站用了Cloudflare的「始终使用HTTPS」功能,HTTP自动跳HTTPS。但随之而来的一个隐蔽问题:Google Analytics里的流量来源变成direct,UTM参数全部丢失

根本原因是:Cloudflare的默认HTTPS跳转不保留查询字符串?utm_source=xxx 在跳转过程中被丢弃了。

方案一:Transform Rules(推荐)

  1. 登录Cloudflare控制台,进入站点
  2. 左侧菜单 RulesTransform Rules
  3. Create rule,类型选 Rewrite URL
  4. 条件:SSL/HTTPSOff
  5. Action:Rewrite to...,填入 https://{host}{uri}
  6. 确保 Preserve query string 开启
  7. Deploy

Transform Rules是内部重写,后端收到的已经是带参数的HTTPS请求,UTM参数完整保留。

方案二:Page Rules(有坑)

  1. Rules → Page Rules → Create Page Rule
  2. URL匹配:http://*yourdomain.com/*
  3. Forwarding URL,目标URL填 https://yourdomain.com/
  4. 状态码:301

必须显式写上?,否则查询参数会丢失。免费版只有3条Page Rules。

方案三:Cloudflare Workers(最灵活)

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
  const url = new URL(request.url)
  if (url.protocol === 'http:') {
    const httpsUrl = 'https://' + url.host + url.pathname + url.search
    return Response.redirect(httpsUrl, 301)
  }
  return fetch(request)
}

将Worker绑定到 yourdomain.com/*url.search 保证查询字符串完整传递。

三种方案对比

方案免费可用保留参数推荐
Transform Rules默认保留5星
Page Rules仅3条需手动配置3星
Workers10万请求/天代码控制4星

验证:用curl确认参数保留

curl -L -v "http://yourdomain.com/?utm_source=test&utm_medium=article"

观察Location头,确认跳转后URL包含完整查询参数。

相关文章

版权声明

本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论