Cloudflare Workers URL Shortener

Phiên bản Cloudflare Workers tạo URL Shortener siêu đơn giản dành cho bạn nào quan tâm

Add trực tiếp đoạn code bên dưới vào Workers

/**
 * For this to work add a route like:
 * https://yourname/*	cloudflare-url-shortener
 */

var urls = {

  "server": "https://raw.githubusercontent.com/bibicadotnet/LCMP-bibicad.net/main/setup.sh",
  "speedtest": "https://raw.githubusercontent.com/bibicadotnet/across/master/bench.sh",
  "lcmp": "https://raw.githubusercontent.com/bibicadotnet/LCMP-bibicad.net/main/public.sh",
  "lcmp_ipv6": "https://raw.githubusercontent.com/bibicadotnet/LCMP-bibicad.net/main/ipv6.sh",

}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
})

async function handleRequest(request) {
  const path = new URL(request.url).pathname.substring(1);
  console.log(request.headers['user-agent'], new Date(), path);
  if (path in urls) return new Response(null, {status: 302, headers: {location: urls[path]}});
  else return new Response('404 not found?', {status: 404});
}

Cấu trúc đơn giản /url_path/ -> link cần tới

"speedtest": "https://raw.githubusercontent.com/bibicadotnet/across/master/bench.sh",

Giờ gõ /speedtest vào là đi thẳng tới https://raw.githubusercontent.com/bibicadotnet/across/master/bench.sh

Muốn thêm link vào thì cứ add thêm, mình demo sẵn 4 link có sẵn cho mọi người tham khảo

Add Custom Domain

2023-09-28_6-00-02

Triggers -> Add Custom Domain, tạo 1 sub domain, kiểu go.bibica.net cho dễ nhớ 😀

Speedtest VPS mình hay dùng giờ ngắn gọn thành như này

wget -qO- https://go.bibica.net/speedtest | bash

Trước đây thì mình hay dùng Pretty Links trên WordPress để tạo các link rút gọn, nó rất là mạnh, lưu khá nhiều thông tin liên quan tới lượt click, tùy chỉnh cách di chuyển …. ẩn được các link reff tránh các con mắt tò mò, giúp bạn kiểm soát các link quảng cáo của mình rất tốt ….

Kiểu có 30-40 lượt click, mà không thấy ai mua hàng, thì cần xem lại coi sao dạo này hàng bán kém thế =))

Nó cũng tích hợp rất mạnh vào WordPress, giúp việc rút gọn link khi viết bài siêu nhanh

Có điều theo thời gian dùng, mình cũng lười theo dõi các giá trị này, bán hàng tử tế đàng hoàng thì cũng không cần phải dấu cái gì 😛 thi thoảng mới tạo 1 link rút gọn nên cũng không quan trọng tự động hay thủ công …. đổi sang Cloudflare Workers cho khỏe

Do dùng cơ sở hạ tầng Cloudflare Edge, nên URL Shortener siêu nhanh, lại không phải cài đặt bất cứ thứ gì liên quan tới server, không lo trang bị sập, làm link rút gọn bị tạch :]]

Về mặt quản trị thì cũng có thể cài thêm KV làm database cho Workers URL Shortener (lên github search tẹo là ra code mẫu), mỗi cái mình thấy phiền, dùng đơn giản thế cũng đủ nhu cầu rồi

Bản miễn phí Cloudflare cho 100,000 request mỗi ngày, quá thừa cho các nhu cầu cá nhân 😛

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback.
Notes: However, those deemed to be spam or solely promotional will be deleted.

You can create a Gravatar account, add avatar, then use that email to comment here, your account will have a more beautiful Avatar, easier to recognize with other members.

Please use real emails, you can receive notifications when comments are replied