效果预览
https://go.rdyu.one/xxx → 自动 301 跳转到你在 KV 里配置的任意目标 URL


1. 创建 KV 命名空间(30 s)

  1. 登录 Cloudflare → Workers & Pages → KVCreate namespace
  2. 名称填:GO_LINKS,记下 Namespace ID

2. 编写并部署 Worker(1 min)

  1. 左侧点 Functions(小 </> 图标)→ Create Service → 名称 go-short → 语言选 JavaScript
  2. 代码(worker.js)一键替换为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export default {
async fetch(req, env, ctx) {
const url = new URL(req.url);
const slug = url.pathname.replace(/^\/+/, "");
if (!slug) return Response.redirect("https://deusyu.app", 301);
const target = await env.GO_LINKS.get(slug);
if (target) {
ctx.waitUntil(
env.GO_LINKS.put(
`log:${Date.now()}:${crypto.randomUUID()}`,
JSON.stringify({ slug, country: req.headers.get("cf-ipcountry") }),
{ expirationTtl: 60 * 60 * 24 * 30 }
)
);
return Response.redirect(target + url.search, 301);
}
return new Response("Not found", { status: 404 });
},
};

Save and Deploy


3. 绑定 KV(30 s)

  1. 在 Service 详情页 → Settings → BindingsAdd binding
  2. 类型选 KV Namespace,变量名填 GO_LINKS,Namespace 下拉选刚建的 GO_LINKS → Save

4. 添加 HTTP Route(30 s)

  1. 同一 Settings 页 → 左侧点 Domains & Routes
  2. HTTP Routes 区点击 Add route
    • Route: go.rdyu.one/*
    • Environment: production
    • Worker: go-short 自动选中 → Save

5. 配置 DNS & HTTPS(30 s)

  1. DNS → Add record
    • Type: CNAME
    • Name: go
    • Target: workers.cloudflare.com
    • Proxy: 橙云 ON
  2. Pages 项目(如你用 Pages 托管)→ Settings → Custom domainsAdd custom domain → 填 go.rdyu.one → Save
    • 或者:直接在 Functions 服务里添加 Custom Domain,具体看你选的部署方式。
  3. 等待几分钟,证书自动就绪。

6. 测试(10 s)

text
1
2
curl -I https://go.rdyu.one/anyslug
# 应返回 HTTP/2 301 Location: https://deusyu.app/anyslug

Done!


📌 后续维护

  • 新增短链:Dashboard → KV → Explore → Add key=xxx value=https://目标
  • 查看流量:Dashboard → KV → Explore → 搜 log: → 导出 JSON/CSV 分析
  • 分流/多语言:在 Worker 里用 cf-ipcountry 或 accept-language 增加分支即可

简单 6 步,3 分钟上线,轻松玩转 Cloudflare 短链神器!