Project Overview
Technical Implementation
1. Database Schema
2. Code Generation & Collision Handling
- The app takes the MD5 hash of the original URL.
- It encodes the hash into a Base62 string (using characters a-z, A-Z, and 0-9).
- It takes the first 6 characters of the Base62 string as the short code.
- Collision Check: The app queries the database to check if that short code is already in use. If a collision is detected (which is rare for a 6-character Base62 space), the app appends a random salt to the URL and re-hashes until a unique code is generated.
3. URL Redirection (Apache Rewrite Rules)
Technology Stack
- Backend Language: PHP 7.4+
- Database: MySQL / MariaDB
- Web Server: Apache (with mod_rewrite enabled)
- Frontend: HTML5, CSS3 (minimal, responsive layout)
Challenges and Solutions
- Preventing Loops: Users could theoretically input the short link of the app itself, creating an infinite redirect loop. I resolved this by adding input validation that matches the domain name of the submitted URL against the host header of the server, rejecting any attempts to shorten URLs containing the app's own domain name.
- SQL Injection: To prevent SQL injection attacks through custom codes, the database queries use PDO prepared statements with parameter binding instead of string concatenation.