Skip to content

Commit

Permalink
Add new post
Browse files Browse the repository at this point in the history
  • Loading branch information
noobnooc committed Aug 9, 2024
1 parent c9f10af commit d284863
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Building a URL Redirect Service with Cloudflare
slug: a-url-redirect-service-with-cloudflare
lang: en
date: 2024-08-09 11:00:00
categories:
- development
- product
description: Building a URL redirect service using Cloudflare Pages and Cloudflare KV

keywords:
- domain redirect
- short links
- url redirect
- Cloudflare Pages
- Cloudflare KV
- Cloudflare
- JavaScript
- Claude
- AI
---

I had a requirement recently because my app contains links to Twitter, GitHub, and Discord, but I didn't want to hardcode these links directly into the app, especially for Discord, as its link seems like it could change at any time. So I needed a more stable link that could then redirect to the target link. My initial idea was to redirect [weel.one/x](https://weel.one/x) to Twitter, [weel.one/discord](https://weel.one/discord) to Discord, [weel.one/spark-ios](https://weel.one/spark-ios) to the App Store link for the Spark Notes app, and [weel.one](https://weel.one) to the company's official website [weelone.com](https://weelone.com). This way, I could achieve stable links forwarding to potentially unstable links, make the purpose of the links immediately apparent, and create short links all at once.

Some domain service providers offer redirect services directly, like the now-defunct Google Domains used to. However, all my domains are on Cloudflare, and Cloudflare's redirect service is particularly complex and has quantity limitations. So I had to implement one myself using Cloudflare Pages. With everyone recently talking about how great Claude is at writing code, I thought it would be perfect to try and have it implement this service from scratch.

This led to the creation of this URL redirect service implemented by Claude, [cf-redirect](https://github.com/noobnooc/cf-redirect), which can be quickly deployed to Cloudflare Pages and uses Cloudflare KV to store the redirect link relationships.

For example, after adding the following key-value pairs to Cloudflare KV:

```
redirect:x = https://twitter.com/weeloneHQ
redirect:github = https://github.com/weelone
redirect:discord = https://discord.gg/BehJCsAqQA
redirect:* = https://weelone.com
```

After that, visiting [weel.one/x](https://weel.one/x), [weel.one/github](https://weel.one/github), [weel.one/discord](https://weel.one/discord) will all be redirected to their corresponding links. Visiting non-existent links or directly accessing [weel.one](https://weel.one) will be redirected to [weelone.com](https://weelone.com).

If you also want to quickly deploy such a service, you can directly check the deployment process in the [repository](https://github.com/noobnooc/cf-redirect).
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: 使用 Cloudflare 搭建一个 URL 重定向服务
slug: a-url-redirect-service-with-cloudflare
lang: zh
date: 2024-08-09 11:00:00
categories:
- development
- product
description: 使用 Cloudflare Pages 和 Cloudflare KV 搭建一个 URL 重定向服务

keywords:
- 域名重定向
- 短链接
- Cloudflare Pages
- Cloudflare KV
- Cloudflare
- JavaScript
- Claude
- AI
---

之前有一个需求,因为在我的 App 里会有 Twitter 、GitHub 和 Discord 的链接,而我又不想直接把链接硬编码到 App 里,尤其是 Discord ,他的链接看起来就像是随时会变的样子。所以我需要一个更稳定的链接,然后再重定向到目标链接。我初步设想的是,可以把 [weel.one/x](https://weel.one/x) 转到 Twitter,[weel.one/discord](https://weel.one/discord) 转到 Discord,[weel.one/spark-ios](https://weel.one/spark-ios) 转到星火记 app 的 App Store 链接,再把 [weel.one](https://weel.one) 转到公司的官网 [weelone.com](https://weelone.com)。这样即使实现稳定的链接转发到不稳定的链接,又能一眼看出链接的目的,还能实现短链。

有些域名服务商会直接提供重定向服务,像已经死去的 Google Domains 就有提供。但是我的域名全在 Cloudflare 上,而 Cloudflare 提供的重定向服务特别复杂,还限制数量。于是只能自己用 Cloudflare Pages 来实现一个了,趁着大家最近再说 Claude 写代码十分厉害,正好可以试试让他从头来实现这个服务。

于是就有了这个由 Claude 实现的 URL 重定向服务,[cf-redirect](https://github.com/noobnooc/cf-redirect) ,可以快速部署到 Cloudflare Pages 上,然后使用 Cloudflare KV 来存储重定向的链接关系。

比如在 Cloudflare KV 里添加了如下键值对:

```
redirect:x = https://twitter.com/weeloneHQ
redirect:github = https://github.com/weelone
redirect:discord = https://discord.gg/BehJCsAqQA
redirect:* = https://weelone.com
```

之后再访问 [weel.one/x](https://weel.one/x), [weel.one/github](https://weel.one/github), [weel.one/discord](https://weel.one/discord) 都会被重定向到对应的链接,访问不存在的链接或者直接访问 [weel.one](https://weel.one) 都会被重定向到 [weelone.com](https://weelone.com)

如果你也想要快速部署一个这样的服务,可以直接到 [仓库](https://github.com/noobnooc/cf-redirect) 里查看部署流程哦。

0 comments on commit d284863

Please sign in to comment.