Skip to main content

Overview

shrtnr converts long URLs into compact, shareable short links that redirect to your original destination. Every short link is automatically validated, stored securely, and cached for fast redirects.

How URL Shortening Works

1

Paste your URL

Enter any valid HTTP or HTTPS URL into the input field on the home page. shrtnr validates that your URL has a proper protocol and format.
2

Generate short code

shrtnr generates an 8-character unique identifier using nanoid. This creates billions of possible combinations to ensure uniqueness.
3

Store and cache

The URL mapping is stored in the database and cached in Redis for lightning-fast lookups. Your short link is ready to use immediately.
4

Share your link

Copy your short link and share it anywhere. When clicked, users are instantly redirected to your original URL.

For Guest Users

Anyone can create a short link without signing in:
  1. Navigate to the home page
  2. Paste your long URL in the input field
  3. Click Shorten
  4. Your short link is automatically copied to your clipboard
Guest links expire after 7 days by default and cannot be customized or managed after creation.

For Authenticated Users

Signed-in users get additional control:
  1. Paste your URL on the home page
  2. Optionally set a custom slug (see Custom Slugs)
  3. Set expiration from 1 to 30 days (defaults to 7 days)
  4. Click Shorten
  5. View and manage your link in the Dashboard
Custom slugs make your links memorable and branded, like yoursite.com/promo2024 instead of yoursite.com/aB3xY7kL.

URL Validation

shrtnr ensures all URLs are valid before creating short links:
  • Protocol check: Only http:// and https:// protocols are allowed
  • Format validation: URLs must be properly formatted and parseable
  • Trimming: Whitespace is automatically removed from both ends
Invalid URLs return an error message: “Invalid URL”. Make sure your URL starts with http:// or https://.

Short Code Generation

The automatic short code generation uses industry-standard nanoid:
  • Length: 8 characters
  • Character set: URL-safe characters (a-z, A-Z, 0-9, -, _)
  • Collision resistance: With 8 characters, nanoid provides ~151 years of continuous generation before reaching 1% collision probability at 1000 IDs per hour
// From app/lib/links.ts:4
const SHORT_CODE_LENGTH = 8;

// Generation happens automatically
shortCode = nanoid(SHORT_CODE_LENGTH);
All short links have an expiration date to keep the database clean and respect privacy:
  • Default expiration: 7 days from creation
  • Minimum: 1 day
  • Maximum: 30 days
  • Automatic cleanup: Expired links return a 404 error and won’t redirect
Expiration dates can be modified after creation in the dashboard. See Link Expiration for details.

Redirect Behavior

When someone visits your short link:
  1. shrtnr checks if the short code exists (reserved routes like /api, /dashboard return 404)
  2. Looks up the original URL in Redis cache for instant response
  3. If not cached, queries the database and validates the link hasn’t expired
  4. Records the click with analytics data (IP, user agent, referrer)
  5. Issues a 307 Temporary Redirect to the original URL
// From app/[shortCode]/route.ts:23
return NextResponse.redirect(originalUrl, 307);
shrtnr uses a 307 redirect instead of 301 to preserve the HTTP method, though most browsers treat GET redirects identically.

Performance & Caching

Redis Caching

Every short link is cached in Redis with automatic TTL (time to live):
  • Cache key format: short:{shortCode}
  • TTL matches the link’s expiration time
  • Cache invalidated when links are updated or deleted

Fast Lookups

The two-tier lookup system ensures maximum performance:
  1. Redis check (microseconds): Check in-memory cache first
  2. Database query (milliseconds): Fallback to database if cache miss
  3. Cache warming: Database results automatically populate Redis
Most redirects complete in under 10ms thanks to Redis caching.

Error Handling

Common errors and their meanings:
ErrorReasonSolution
”Invalid URL”URL doesn’t have http/https or is malformedCheck your URL starts with http:// or https://
”Slug already in use”Custom slug is takenChoose a different custom slug
”Sign in to use a custom slug”Tried to use custom slug as guestCreate an account or sign in
404 on redirectLink expired or doesn’t existVerify the short code is correct

Best Practices

URL Shortening Tips

  • Test your original URL works before shortening
  • Use custom slugs for branded, memorable links
  • Set longer expiration times for links you’ll use repeatedly
  • Track link performance in your dashboard
  • Delete links you no longer need to keep your dashboard organized

Build docs developers (and LLMs) love