Lesson 8 of 15
31. Design a URL Shortener

31.7 301 or 302?

3 min read+10 XP

Both send the browser to the long URL. The difference is what happens on the second click.

a 301 is remembered by the browser; a 302 comes back to us every time.

Figure: with a 301 the browser may skip us next time. With a 302 every click comes through us.

301 Moved Permanently302 Found
Browser may cache it by defaultYesNo (only if headers say so)
Second click comes to usOften notYes
We can count every clickNoYes
We can change or disable the link laterHard (browsers remember)Yes
Load on our serversLowerHigher

Our pick: 302, because the requirements include click counts, and because we may need to disable a link later (for example, if it turns out to point to malware). If analytics weren't needed, 301 would cut our load.

Interviewer pushback:

  • "What about 307 and 308?" → They're the same as 302 and 301 but promise the browser keeps the same method (a POST stays a POST). For simple link clicks, which are GETs, 302 and 301 are fine.

Memory code: "301 is remembered, 302 is reported."