Redirect and Tracking Scripts

CGI redirect scripts: logging clicks and referrers before forwarding the browser

How does a CGI redirect script log a click before forwarding to the destination?

A CGI redirect script reads the destination URL from a query parameter or an internal lookup table, records the visit (timestamp, referrer, user agent) to a log file or database, then immediately sends a Location header with a 301 or 302 status code and exits. The browser follows the redirect to the destination without waiting for any page body, so the logging adds no visible latency from the user's perspective.

Request custom development Getting started

The mechanics of a CGI redirect

The critical detail is that HTTP redirects are entirely in the headers. A CGI script sends a Status header (or relies on the server to infer it from Location) and a Location header with the target URL, then exits without printing a body. The browser reads the status code, follows the Location, and loads the destination. Done correctly, the user sees only the final destination; the logging happens invisibly in between.

RealRefer, one of the original CGI products on this domain, used exactly this pattern to track referrer statistics for affiliate links and outbound clicks. The script received the destination identifier, logged the incoming referrer header, and redirected. This was the standard way to do affiliate click tracking before server-side analytics platforms existed.

Open redirect vulnerabilities

An open redirect is a redirect script that forwards the browser to any URL passed as a parameter, without restriction. Attackers use open redirects in phishing: they link to your trusted domain with a redirect parameter pointing to a malicious site. Because the link appears to go to your domain, email filters and users are more likely to trust it.

The fix is to validate the destination against an allowlist of permitted domains or paths before redirecting. If your script only needs to redirect to your own pages, validate that the destination starts with your own origin. If it must redirect to a fixed set of partner URLs, maintain an internal mapping from opaque IDs to destination URLs and never accept a raw URL as a redirect target from user input.

301 vs 302 and when to use each

A 301 Moved Permanently tells the browser and search engines that the destination URL is the permanent home of this resource. The browser caches the redirect, and search engines transfer link equity to the destination. Use 301 for permanent link aliases and for any URL you intend to keep pointing at the same place long term.

A 302 Found (or 307 Temporary Redirect) tells the browser the redirect is temporary and not to cache it. Use 302 for dynamic redirects where the destination changes per request (like a tracking link that could point anywhere), for A/B test routing, and for login redirects that send the user back to the page they were trying to visit. For click tracking, 302 is the correct choice because you do not want the browser to cache and bypass the tracking script on repeat visits.

What to know

Key points

Get help

Listings and a local agent, when you are ready

Need a CGI script built, debugged, or a hosting environment configured? Forms use a clearly-marked placeholder endpoint until wired to a real system.

Hosting partner CGI-compatible hosting for link tracking

Reserved for an affiliate link to a vetted shared or VPS hosting provider that supports CGI and Perl/Python execution. Operator wires affiliate link here.

Development Need a custom CGI script built?

Self-hosted lead form for custom CGI development inquiries. Placeholder endpoint until wired to the operator's CRM or email handler.

Open inquiry form →

Custom CGI development

No spam. Your inquiry is answered within 1 business day.

Hosting setup help

No spam. Your inquiry is answered within 1 business day.

Questions

Frequently asked questions

What is an open redirect and why is it dangerous?
An open redirect forwards the browser to any URL passed as a parameter without checking whether it is a permitted destination. Attackers use this in phishing: a link to your domain with a redirect parameter pointing to a malicious site looks trusted in emails. The fix is to validate the redirect destination against an allowlist of permitted domains or paths, or use an opaque ID that maps to a pre-approved URL list server-side.
How do I track affiliate link clicks with a CGI script?
The script receives a click identifier in the query string, looks up the destination URL for that ID in its mapping file, writes a log entry (timestamp, referrer header, user agent), and sends a 302 redirect to the destination. Use an opaque ID as the parameter, not the raw destination URL, to avoid open redirect vulnerabilities. The log entry records where the click came from so you can attribute traffic to sources.

CustomCGI publishes reference information on CGI programming and server-side web scripting for educational and informational purposes. Code examples are provided as-is for learning; always audit and test scripts in a staging environment before deploying to production. Security vulnerabilities in CGI scripts are a real risk; follow current best practices and review the security guide before deploying any user-facing code.