Software, your way.
burger menu icon
WillMaster

WillMasterBlog > HTML

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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: https://example.com/page1.php
301 Destination: https;//example.com/page2.php

The first time the original URL is requested, the browser:

  1. Tries to load the content at the original URL: https://example.com/page1.php

  2. Is redirected to the destination URL: https;//example.com/page2.php

During subsequent times the original URL is requested, the browser:

  1. 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: https://example.com/page3.php
301 Destination: https;//example.com/page4.php

The first time the original URL is requested, the browser:

  1. Tries to load the content at the original URL: https://example.com/page3.php

  2. Is redirected to the destination URL: https;//example.com/page4.php

During subsequent times the original URL is requested, the browser:

  1. Tries to load the content at the original URL: https://example.com/page3.php

  2. 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

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

Simple Floating Menu

When not needed, the menu on the left side of a web page retracts to be out of the way. Tap it and it expands.

CSS Drop Shadows for Images

Images can be given drop shadows using pure CSS.

Ajax File Upload

Learn how to upload a file as soon as a file upload form field is given a file to upload.

Another Form Spam Prevention Technique

This technique should stop much or most of the form spam you are experiencing, perhaps even all of it.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC