How it works
For a valid key, respond with a redirect and a Location header pointing to the saved destination. A 301 expresses a permanent move. HTTP treats it as cacheable by default, so browsers or intermediaries can reuse it and skip your service. A 302 expresses a temporary move and is not cached unless the response says so, which suits links whose clicks you count or whose destination may change. Cache-Control headers can override both defaults, and neither status guarantees an exact analytics count, because bots and prefetching also produce requests.
Follow one visitor who opens a link five times in a week. With a 301 and no cache headers, the browser may contact your service only on the first click: analytics records one visit, and if the owner repoints the link, that browser may keep going to the old page. With a 302, all five requests reach you, which gives five chances to log and update but keeps that traffic on your servers. A CDN such as CloudFront can cache either response, cutting load but moving click data into its logs. A 301 fits a link that will never change and whose clicks nobody counts.
Check expiry before redirecting. An unknown key can return 404; a known, deliberately expired link may return 410 Gone. Never redirect an expired link to an old destination, and keep cache lifetimes (often called TTL, time to live) shorter than the link’s remaining life: a one-hour max-age on a link expiring in ten minutes can outlast it by 50 minutes.
The product needs accurate per-click reporting and destinations can change. Which redirect is safer?
Use a temporary redirect such as 302, with cache headers chosen for the reporting and update requirements. A permanent redirect can let a browser bypass the service on later visits, so those visits may never reach your analytics pipeline.
Use this as a guide for self review; written answers are not graded.Keep in mind
- 301 and 302 are product and caching decisions, not just performance switches.
- TTL and error responses are part of the public link contract.