30分鐘,做一個自己的短網址服務

利用 Google Sheets 當資料庫,透過 AppScript 建立 API,讓 Cloudflare Workers 當你的轉址小精靈!

為什麼我需要這個酷東東?

  • 不想跟別人用一樣的短網址(例如:reurl、bitly…)
  • 想要有自己的短網址(cool.page/home 之類的不是超酷嗎?)
注意:免費版 Cloudflare Workers 有 100,000 請求 / 天 的限制,如果會超過這個用量請自行付費升級,但如果是個人或小公司應該是夠用的~

預期流程

第 0 步:準備一個網域

  • 由於本教學會使用 Cloudflare Workers,建議一起在 Cloudflare 購買網域,之後設定 DNS 會比較方便,而且 Cloudflare 年費公道,也沒有那些奇奇怪怪的行銷花招。
Cloudflare 購買網域教學
    1. 點擊「Register Domains」
    2. 輸入想要購買的網域(建議越短越好,不然就不叫短網址了)
    3. 按下搜尋
    4. 買一個符合自己需求、預算的網域

第 1 步:建立 Google Sheets

  1. 開一個 Google Sheets
  2. 參考下方格式製作表格,左邊放短碼,右邊放原始網址:
shortcode target_url
gh https://github.com
example.com/gpt 為例,shortcode(短碼)就是 「gpt」

第 2 步:建立 JSON API(Google Apps Script)

  1. 在 Google Sheets 點選「擴充功能 → Apps Script
  2. 將預設程式碼取代為以下程式碼:
function doGet() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("工作表1"); // 請改為你的工作表名稱
  const data = sheet.getDataRange().getValues();

  const result = {};
  for (let i = 1; i < data.length; i++) {
    const shortcode = data[i][0];
    const targetUrl = data[i][1];
    if (shortcode && targetUrl) {
      result[shortcode] = targetUrl;
    }
  }

  return ContentService
    .createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

  1. 點選「部署 → 新增部署作業」
CleanShot 2025-06-10 at 21.32.13.png
  • 選取類型:網頁應用程式
  • 執行身份:我
  • 誰可以存取:所有人
第一次部署會需要授予存取權,雖然看起來有點可怕,但勇敢地按 Allow 就對了
  1. 部署完成後會拿到一個網址,例如:
https://script.google.com/macros/s/AKfycbxxxxxxxxxxxxxxxxxxxxxx/exec

記下來!後面 Workers 會用到

CleanShot 2025-06-10 at 21.36.57.png

第 3 步:建立 Cloudflare Worker

  1. 前往 https://workers.cloudflare.com → 登入
  2. 建立一個新 Worker,名稱例如 go-redirect
  1. 將 worker.js 內的預設程式碼替換為下方程式碼
//API_URL 換成剛剛在 AppScript 拿到的網址
const API_URL = "https://script.google.com/macros/s/AKfycbxxxxxxxxxxxxxxxxxxxxxx/exec";

export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);
    const shortcode = url.pathname.slice(1);

    const cache = caches.default;
    const cacheKey = new Request(API_URL, { method: 'GET' });

    try {
      // 嘗試從快取讀取
      let response = await cache.match(cacheKey);

      if (!response) {
        // 沒有快取 → 抓 JSON
        response = await fetch(API_URL, {
          headers: { "Cache-Control": "no-cache" },
        });

        if (response.ok) {
          const copy = response.clone();
          ctx.waitUntil(cache.put(cacheKey, copy)); // 非同步寫入快取
        } else {
          return new Response("無法取得資料", { status: 502 });
        }
      }

      const redirects = await response.json();

      if (redirects[shortcode]) {
        return Response.redirect(redirects[shortcode], 302);
      } else {
        return new Response("短碼不存在", { status: 404 });
      }
    } catch (e) {
      return new Response("錯誤:" + e.message, { status: 500 });
    }
  }
}

  1. 部署 workers:點「Deploy」,記住https://go-redirect.<your Subdomain name>.workers.dev,之後會用到
CleanShot 2025-06-10 at 21.46.26.png

5. 測試:進入 https://go-redirect.<your Subdomain name>.workers.dev/gh ,應該會跳轉到 https://github.com/

第 4 步:綁定自訂網域

  1. 到你的 Cloudflare 帳戶 → 選取對應的網域(例如 example.com
CleanShot 2025-06-10 at 21.51.00.png
  1. 在 DNS 新增一筆:
// 想要未來的短網址長 go.example.com/shortcode 就這樣設定
類型:CNAME
名稱:go
目標:你的 Worker 子網域(例如 go-redirect.<your Subdomain name>.workers.dev)

// 想要未來的短網址長 example.com/shortcode 就這樣設定
類型:CNAME
名稱:@
目標:你的 Worker 子網域(例如 go-redirect.<your Subdomain name>.workers.dev)
  1. 然後在 Workers → Routes 中設定:
// 想要未來的短網址長 go.example.com/shortcode 就這樣設定
區域:example.com
路由:go.example.com/*

// 想要未來的短網址長 example.com/shortcode 就這樣設定
區域:example.com
路由:example.com/*

第 5 步:大功告成!

現在,當你訪問 https://<你的短網址>/gh ,應該會跳轉到 GitHub,快去試試看吧!