Total Pageviews

Sunday, 14 June 2026

一个短网址服务:gh-pages-url-shortener,它可以缩短任何长网址

 Minimal URL shortener that can be entirely hosted on GitHub pages. 

This is a minimal URL shortener that can be entirely hosted on GitHub pages. It does not need the maintenance of any servers or databases and can be hosted entirely on GitHub for free!

  1. To add a new short link, add an issue with the title being the link you want to shorten (including the http(s)://) to https://github.com/nelsontky/gh-pages-url-shortener-db/issues.

  2. The newly created short url can be accessed via ccb.wtf/{issue_number}

☕️ Features

  1. Unlike many URL shorteners, this one does not need a database uses a "database" in the form of GitHub issues and can be entirely hosted on GitHub pages.

  2. There is no need for the pound symbol - short URLs look clean like this: ccb.wtf/1 instead of looking like this: ccb.wtf/#1.

💡 How does this work?

Thanks to @kidGodzilla for the pretty neat explanation here.

  1. 404.html handles all requests
  2. Small javascript snippet fetches a JSON representation of the GitHub issue via the JSON API, and redirects to the issue title, as a URL.

😎 This is so cool! How can I use this with my own domain?!

Disclaimer: This method of creating a URL shortener is hacky and not meant to be reliable. Do proceed at your own risk!

  1. Fork the repo before cloning your fork.
  2. Set up GitHub pages for your forked repo.
    1. In your forked repo, click the Settings tab and scroll down to the GitHub Pages section.
    2. Then select the main branch source and click on the Save button.
    3. How to create GitHub page
  3. If you are using your own domain:
    1. Set your domain up for GitHub pages.
    2. Change the URL in CNAME file to your domain.
  4. If you are using GitHub page's default domain i.e. Something like https://<username>.github.io/<repo-name>/
    1. Delete the CNAME file.
    2. Change var PATH_SEGMENTS_TO_SKIP = 0; at the top of 404.html to var PATH_SEGMENTS_TO_SKIP = 1;.
      1. This is as GitHub domains have an additional path segment (the repo name) after the host name.
  5. Create a new repo as a database. (Or you could use your forked repo)
    1. Update var GITHUB_ISSUES_LINK = "<your-github-issues-link>"; at the top of 404.html accordingly afterwards.
      1. Format for GITHUB_ISSUES_LINK: https://api.github.com/repos/{owner}/{repo}/issues/
      2. Remember the trailing /!
  6. Push your changes to your forked repo, and your low cost and cool as heck URL shortener will be ready for use!

🍴 Featured forks

To feature your fork here, edit this section and open a PR!

  • eexit.github.io/s - Created a bash script that allows for shortening of URLs straight on the command line! Check out his script here.
  • gh-short-url - A npm command line tool that uses GitHub pages to convert short URLs.

from  https://github.com/nelsontky/gh-pages-url-shortener

( https://github.com/brightmann/url-shortener)

示例: https://github.com/nelsontky/gh-pages-url-shortener-db/issues/1894 ,

然后访问 https://ccb.wtf/1894 即可跳转到

https://www.youtube.com/watch?v=qW5_A_zUjlw

----------------------------------------------------------------

使用 GitHub 搭建自己的短链接服务

 使用两个 GitHub 仓库和一个域名就能拥有免费的短链接服务。

前两天偶然在 GitHub 发现一个挺有意思的项目,可以不依赖自己的服务器、数据库来构建一个短链接服务。自己尝试了一下,还挺简单的。这里记录一下自己的构建流程,感兴趣的小伙伴可以自己尝试一下。
Prerequisites

    新建两个 GitHub 仓库,一个用来做服务器存储源码、提供服务(url_shortener),一个用来做数据库存储链接(url_shortener_db)
    注册一个域名(可选),如果没有的话,可以直接使用 GitHub pages 的域名(username.github.io)。
获取及配置源码

首先,你需要获取这个服务的源代码,你可以直接 fork 这个源码仓库-

https://github.com/nelsontky/gh-pages-url-shortener

然后,在你的GitHub 仓库网页(我的是https://github.com/brightmann/url-shortener)上操作,修改404.html文件的GITHUB_ISSUES_LINK字段,指向自己的url_shortener_db仓库,这个仓库的issues就是作为存储你的链接的数据库:
var GITHUB_ISSUES_LINK =
"https://api.github.com/repos/username/repo-name/issues/";
注意把上面的username repo-name替换为自己的用户名及仓库名。还有,最后的/不要漏掉,这是获取链接的关键。我的情形是

https://api.github.com/repos/brightmann/url_shortener_db/issues/

配置 GitHub Pages

接下来就是配置 Github Pages 了,这部分也很简单。

点击仓库的Settings选项,找到其中的GitHub Pages部分,然后配置Source中的分支: 



下面的部分根据自己需要选择:
如果你不使用自己的域名:
    删除仓库中的CNAME文件
    把404.html文件中的var PATH_SEGMENTS_TO_SKIP = 0;改为var PATH_SEGMENTS_TO_SKIP = 1;
    到这里就结束了。

测试效果

    在你开始作为数据库的 GitHub仓库(我的是

https://github.com/brightmann/url_shortener_db)创建一个 issue,标题就是你需要缩短的某个长网址,其他什么都不需要做,直接 Submit 就行了。我在此

https://github.com/brightmann/url_shortener_db/issues/1 创建了一个issue.
    等待一会儿,然后在浏览器访问:https://brightmann.github.io/url-shortener/1 ,就会跳转到https://www.youtube.com/watch?v=sNYVlOhMgIg

有人在这里-https://github.com/nelsontky/gh-pages-url-shortener/issues/5#issuecomment-728040879给了一个比较好的解释,大家可以参考一下。

参考:
https://github.com/nelsontky/gh-pages-url-shortener


 

No comments:

Post a Comment