init
This commit is contained in:
80
README.md
80
README.md
@ -1,3 +1,79 @@
|
||||
# shortener
|
||||
# Micro URL Shortener
|
||||
|
||||
A single file URL shortener in PHP. Designed for self hosted personal use.
|
||||
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`):
|
||||
|
||||
```apache
|
||||
RewriteEngine On
|
||||
RewriteRule ^(urls|last)\.txt$ - [F]
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule ^ index.php [L]
|
||||
```
|
||||
|
||||
**nginx**:
|
||||
|
||||
```nginx
|
||||
location ~ ^/(urls|last)\.txt$ { deny all; }
|
||||
location / { try_files $uri /index.php?$query_string; }
|
||||
```
|
||||
|
||||
**Local development** (PHP's built-in server, using `index.php` as the router):
|
||||
|
||||
```sh
|
||||
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. |
|
||||
| `$urls_file`, `$last_file` | `urls.txt`, `last.txt` | Where the data is stored. |
|
||||
|
||||
## API
|
||||
|
||||
Shorten a URL from the command line. The response is plain text:
|
||||
|
||||
```sh
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user