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.
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
- Validate the destination against an allowlist. Never accept a raw URL from user input as a redirect target; use an internal ID-to-URL mapping or a domain allowlist.
- Use 302 for tracking redirects, 301 for permanent moves. A cached 301 bypasses your tracking script on repeat visits; use 302 for dynamic tracking links.
- Log before redirecting, not after. CGI exits immediately after sending the redirect; there is no after. Write to the log file before printing the Location header.
- Include a short body as a fallback. Some old browsers do not follow Location headers; a brief HTML body with a manual link covers them.
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.
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.
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
Hosting setup help
Questions