Guide

When someone opens a short link, the server replies with a redirect. That reply has a status code and a Location header holding the long URL. The status code is also a promise about the future. It tells the browser and any CDN whether they may remember the answer, and that decides whether your server sees the next click.

Concept

Before you start

  • HTTP response: the server's reply to a request, made of a status code (such as 200 or 404) and headers.
  • Cache: a stored copy of an answer that can be reused without asking the server again. See caching.

The big idea

Picture a shop sign that says "we moved to 12 Oak Street." If the sign says the move is permanent, regulars note the new address and stop coming here. If it says the move is temporary, they check the sign again next time.

Browsers do not read signs, but they follow the same logic through the status code.

StatusBrowser and CDN behaviorClick countsUse when
301 PermanentMay cache the answer for a long time and skip your serverRepeat clicks are missedYou do not need click counts
302 or 307 TemporaryAsks again on each visit, or after a short cacheEach click can be loggedYou need analytics or expiry

The picture breaks in one place. A 302 with a cache header still hides some repeat clicks. The header decides how long.

Walking through it

Suppose a 302 comes with Cache-Control: private, max-age=60. Here is one visitor's browser over time:

t = 0 s    browser → server      server replies 302, browser caches it for 60 s
t = 10 s   browser → cache       no request reaches your server
t = 75 s   browser → server      cache expired, so this click is counted

The private part means only that browser may keep the answer. A shared CDN will not. The 10-second click is invisible to you, but you still see the click at 75 seconds. Choose the max-age knowing how much you are willing to miss.

Common mistakes

  • Using 301 "for speed" and then wondering why click counts fall. Browsers and CDNs may serve repeat clicks without telling you.
  • Assuming a 302 means every click arrives. A max-age header can absorb clicks within the window.
  • Caching a 301 at the CDN with a long TTL. Expiry and takedowns then take effect slowly or never.

Check yourself

1. A 301 response is cached at a CDN for a year. Why do yesterday's click counts look low?

Answer: Repeat clicks were served from the cache, so they never reached your server to be counted.

2. You need click counts and you want expired links to stop working. Which code do you return?

Answer: A 302 or 307, so browsers ask again and your server can return 404 or 410 once a link expires.

Key takeaways

  • The status code is a promise about caching, not just a label.
  • 301 lets browsers and CDNs stop calling you. 302 and 307 keep you in the loop.
  • For a link product that counts clicks or expires links, use 302 or 307.
  • Even with 302, a max-age hides clicks inside its window, so choose it on purpose.

Further learning

Next, see how the codes themselves are minted in short code generation.