2026-11-28 11:21:08 -05:00
2026-11-28 11:21:08 -05:00
2026-11-28 10:32:52 -05:00
2025-09-30 21:24:53 -04:00
2026-11-28 11:21:08 -05:00
2026-11-28 11:21:08 -05:00
2026-11-28 11:21:08 -05:00
2026-11-28 10:32:52 -05:00

Micro URL Shortener

A tiny URL shortener in a single PHP file (index.php). It needs no database: links are stored in plain text files.

How it works

  • urls.txt: one link per line, written as <key> <url>.
  • last.txt: the most recently issued key. Each new key is made by incrementing it over a shuffled character set (a, c, r, … then aa, ac, …).
  • Visiting /<key> sends a 301 redirect to the stored URL, or returns 404 if the key is unknown.
  • Visiting / shows a mobile-friendly form for shortening a URL.

Requirements

  • PHP 7+ with the mbstring extension.
  • The web server must be able to write urls.txt and last.txt in the app directory.
  • Every request path must be routed to index.php (see below).

Setup

  1. Copy index.php to your web root.
  2. Edit the configuration at the top of the file. At minimum, change $password.
  3. Route all requests to index.php.

Apache (.htaccess):

RewriteEngine On
RewriteRule ^(urls|last)\.txt$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

nginx: use the included nginx.conf, which goes inside your http block (e.g. in sites-enabled). It:

  • sends every request to index.php, so nothing else in the directory (urls.txt, .git, …) is served directly
  • rate-limits each IP to 10 requests/second (burst 20) overall, and to 10/minute (burst 5) for creating links or trying passwords. Over-limit requests get 429.

It expects an upstream php { ... } pointing at PHP-FPM. If nginx is behind another proxy, uncomment the real_ip lines. Otherwise every visitor shares the proxy's rate limit.

Local development (PHP's built-in server, using index.php as the router):

php -S localhost:4000 index.php

Block direct access to urls.txt and last.txt (as in the rules above). Otherwise anyone can download your whole link list.

Configuration

Variable Default Purpose
$password 'change me!' Required to shorten a URL. Set to '' to disable (not recommended).
$allowed_domains [] Domains allowed as link targets. An empty list allows any domain.
$max_url_length 100 Longer URLs are rejected.
$charset (shuffled) Characters used in keys. Don't change it once links exist.
$enable_test false Turns on the key-generation test form.
$auth_cookie, $auth_cookie_days shortener_auth, 365 Cookie that remembers a correct password, and how long it lasts.
$urls_file, $last_file urls.txt, last.txt Where the data is stored.

Remembered password

If you tick Remember me for a year when entering the correct password, the app sets a cookie that lasts one year. While it's valid, the form doesn't ask for the password and a Log out link appears at the bottom of the page. Logging out clears the cookie. The cookie holds an HMAC token derived from the password, not the password itself, so changing $password signs everyone out. The cookie is only accepted on POST requests, so a link to /?url=... on another site can't create links using your cookie.

API

Shorten a URL from the command line. The response is plain text:

curl --data-urlencode 'url=https://example.com/page' \
     --data-urlencode 'password=your-password' \
     https://your-host/
# https://your-host/c
Status Meaning
200 Success; body is the short URL
401 Wrong or missing password
403 Domain not allowed
422 Invalid or overlong URL

GET parameters work too (/?url=...&password=...), but they put the password in server logs and browser history, so prefer POST.

Short URLs always start with https:// followed by the request's host.

Description
A single file URL shortener in PHP. Designed for self hosted personal use.
Readme CC-BY-SA-4.0 50 KiB
Languages
PHP 100%