301 vs 302 vs 303 vs 307 vs 308? Which redirection code should I use for my short URLs

cover-what-the-code

Tldr

If forwarding form submissions, use 307. Otherwise use 302. Only use 301 when it is in fact permanent and you don't care about analytics.

Difference between all the redirection codes

HTTP status codes for a redirection response can be more complicated than you think. When it comes to short URLs, the usual way people use to refer to the codes are "302 temporary" and "301 permanent". While it is true that 302 is indeed a temporary redirection code and 301 does indicate a permanent redirection, this is not the whole story.

In fact, there are 3 different HTTP status codes for a temporary redirection and 2 different codes for permanent ones.

To give a short summary, the status code 302, 303 and 307 are all temporary redirection codes. Let's talk about 303 and 307 first, since 302 has it's own complicated history. The difference between 303 and 307 is that when redirecting using 303, the HTTP method on the target URL will be GET regardless what the original HTTP method is. To give an example, when you submit a form to the URL: http://a.com/form-submit with a POST method, if the server returns a 303 status code to point to http://b.com/form-submit, the data submitted in the form will not be passed along to http://b.com/form-submit, but it will behave as if you directly visited http://b.com/form-submit by typing it in your browser's address bar. And the status code 307 does pass along the submitted form data and keeps the request method.

So, what about 302? Well, 302 was designed to work as 307, but because everyone get it wrong, it in fact behaves like 303. 303 and 307 was invented after 302 to explicitly show the difference. Practically 302 is 303 but is more, a lot more, popular.

Similarly, 301 and 308 is the counter part of each other where 301 always results in a GET request and 308 keeps the original request method.

What's best for short URLs

When it comes to short URLs, it is mostly recommended to use a temporary redirection code, base on the following reasons:

  1. It allows for the target URL to change, because permanent status codes are permanent, and once a short URL is permanently redirected to one user, it cannot be then changed to point to another.
  2. It collects clicks counts more accurately. Again, because permanent status codes are permanent, browsers will remember the redirections forever and the visits will not go through short URL services redirection server for these visits to be recorded.

However, this is not to say to never use a short URL with a permanent redirection code, but it should only be used for a few niche cases:

  1. The redirection speed must be at the optimal level. Permanent redirections performs better because it is remembered by the browser.
  2. The short URL is actually meant to be a permanent redirection and the analytics is not so important.

So temporary codes are better, but which one? 302, 303 or 307? The rule of thumb is that if you need to forward form submissions, use 307. Otherwise just use 302. Why not 303? Because 302 is the defacto standard, it is mostly widely used, most commonly supported, and well understood by all HTTP clients and servers.

Taged with: