Permanent and Temporary Redirects
When should a browser be permanently redirected? And when should it be temporary?
Redirects may be because a web page has moved, for counting clicks, and for other reasons. This article's focus is not on why a redirect might be needed, but on deciding which redirects to use when the choice is between permanent and temporary.
Generally, page redirects are done with the .htaccess
file. The file kicks in right before the page at the requested URL would be delivered to the browser or robot.
Click counter software generally redirects from within the software rather than with the .htaccess
file. If written with PHP, the "Redirecting With PHP" method at the web page linked in the previous paragraph is likely the way your click counting software does its redirecting.
A permanent redirect generally is specified when the new destination is assumed the original URL is unlikely to ever again be the valid location.
A temporary redirect generally is specified when either of these are true:
-
The original URL is likely to be restored as the valid location at some time in the future.
-
The original URL is a click tracker or other statistics-gatherer. The statistics are gathered and then the browser is directed to the destination URL.
The browser follows different steps when it complies with different redirects.
Permanent Redirect —
When a browser is permanently redirected (redirect code 301 or code 308), the browser remembers the destination for a time. Thus, when the same original URL is requested again, the browser automatically goes to the redirected destination without even trying the original URL.
As an example, let's assume this:
Original URL:
301 Destination:
The first time the original URL is requested, the browser:
-
Tries to load the content at the original URL:
https://example.com/page1.php -
Is redirected to the destination URL:
https;//example.com/page2.php
During subsequent times the original URL is requested, the browser:
-
Loads the destination URL:
https;//example.com/page2.php
If the URL you are redirecting is accessed with method post, use redirect code 308 instead of code 301. Redirect code 308 ensures the rediect is method post if the original request was method post. (Redirect code 301 should redirect method post if the original request was method post, but some browsers turn method post into method get when the redirect code is 301.)
Temporary Redirect —
When a browser is temporarily redirected (redirect code 302 or code 307), the browser does not make a special effort to remember the destination. Whenever the same original URL is requested, the browser tries the URL and, if a redirected, goes to the destination URL.
As an example, let's assume this:
Original URL:
301 Destination:
The first time the original URL is requested, the browser:
-
Tries to load the content at the original URL:
https://example.com/page3.php -
Is redirected to the destination URL:
https;//example.com/page4.php
During subsequent times the original URL is requested, the browser:
-
Tries to load the content at the original URL:
https://example.com/page3.php -
Is redirected to the destination URL:
https;//example.com/page4.php
If the URL you are redirecting is accessed with method post, use redirect code 307 instead of code 302. Redirect code 307 ensures the rediect is method post if the original request was method post. (Redirect code 302 should redirect method post if the original request was method post, but some browsers turn method post into method get when the redirect code is 302.)
Choices —
If the redirect you are contemplating will be to a permanent destination, use redirect code 301 or, if the request is method post, use redirect code 308. Redirect code 308 can be used for any permanent redirect, whether method post or other method. Redirect code 301 may be specified if it is unlikely that method post will be used.
On the other hand, if the redirect will be temporary or needs to go through a click tracker, use redirect code 302 or, if the request is method post, use redirect code 307. Redirect code 307 can be used for any temporary or tracking redirect, whether method post or other method. Redirect code 302 may be specified if it is unlikely that method post will be used.
(Method post is generally used when submitting a form.)
If you wish to have more information about the above-mentioned redirect codes and also a few others, see HTTP response status codes at the mozilla.org website.
(This content first appeared in Possibilities newsletter.)
Will Bontrager