Skip to content

Redirect to Original URL

Redirect to Original URL

Path: /r/{code}
Method: GET

The Redirect endpoint takes a short code and forwards users to the corresponding original URL. It also logs metadata for each access, enabling click tracking and analytics.

Use Cases:
- Sharing shortened URLs while logging click data.
- Expiring links for limited-time offers or events.
- Collecting analytics on user engagement (IP, device, referer).

Request - example: /r/HrTBms

Response - example: Redirects to the original URL (in this case: https://google.com).

How It Works:
1. Receives a short code in the path.
2. Queries the database for the corresponding URL record.
3. Checks if the URL exists and if it has expired.
4. If valid, creates a click record containing IP address, user agent, referer, and timestamp.
5. Redirects the client to the original URL using an HTTP redirect (302/307).

Implementation Highlights:
- Uses FastAPI’s RedirectResponse to handle HTTP redirection.
- Clicks are logged asynchronously with SQLAlchemy AsyncSession for non-blocking performance.
- Expiration check prevents redirection if the URL has expired (returns HTTP 410).
- Supports logging user metrics for analytics dashboards (IP, user agent, referer).