A simple, beginner-friendly URL shortener built with Python, Flask, and SQLite.
| Feature | Description |
|---|---|
| Shorten URLs | Paste any http:// or https:// URL → get a 6-char short link |
| Redirect | Visit /<short_id> → automatically redirected to the original URL |
| Duplicate detection | Same URL submitted twice? Reuses the existing short code |
| URL validation | Rejects empty or malformed input with clear error messages |
| Recent links list | Homepage shows all previously shortened URLs |
| SQLite storage | Zero-config, single-file database (database.db) |
URL shortener/
├── app.py # Main Flask application
├── database.db # SQLite database (auto-created on first run)
├── requirements.txt # Python dependencies
├── test_app.py # Quick smoke tests
├── README.md
├── static/
│ └── style.css # Dark glassmorphism UI styles
└── templates/
└── index.html # Jinja2 homepage template
pip install -r requirements.txtpython app.pyhttp://127.0.0.1:5000
| URL | Purpose |
|---|---|
https://www.google.com/search?q=flask+tutorial |
Long query string |
https://github.com/pallets/flask |
Clean GitHub URL |
https://en.wikipedia.org/wiki/URL_shortening |
Wikipedia article |
https://docs.python.org/3/library/sqlite3.html |
Python docs |
- User submits a URL via the form on the homepage.
- Validation — a regex checks the URL starts with
http://orhttps://. - Duplicate check — if the URL was shortened before, return the existing short code.
- Generate short ID — pick 6 random alphanumeric characters; retry on collision.
- Store the mapping
(short_id → original_url)in SQLite. - Redirect — when someone visits
/<short_id>, look it up and issue a302 redirect.
With the server running in another terminal:
python test_app.pyExpected output:
[PASS] Redirect test: status=302, location=https://www.google.com/search?q=flask+tutorial
[PASS] Invalid ID test: status=302 (redirect to homepage)
[PASS] Duplicate URL reuse test: 2 rows in DB (expected 2)
All tests passed!