Redirecting a page - silent, temporary or permanent?
Archive - Originally posted on "The Horse's Mouth" - 2010-08-03 16:37:44 - Graham Ellis
To 301 or not to 301 ... that is the question. Whether 'tis better to quietly redirect your users to a new page through mod_rewrite, or to instruct them to look elsewhere - Darren Shakespeare - 2003 ;-)
In all seriousness - what should you do when someone arrives at your web site looking for a URL that doesn't exist? You could simply send back a "not found" message through the HTTP response - type 404 - but there are other alternatives.
• You could use mod_rewrite to silently transfer their request to a different URL that does exist
• You could reply with a status code 302 (or 303 or 307) telling the browser that the resource is temporarily to be found elsewhere
• You could reply with a status code 301 - a permanent move, again telling the browser to re-request from elsewhere.
Which would you use in what circumstances?
* The archive pages of this blog each have their own URL, but in practise there isn't a full directory of files - one for each page - on the server. We use mod_rewrite to silently transfer the requests. And we do it silently because we do not want to change what's in the URL bar, nor to give the user any thought of changing to a different URL.
Example from .htaccess: RewriteRule ^(.*)\.htm dvrt.php?sharename=&%{QUERY_STRING}
* We use a permanent move where people (or automata) used a URL on our wiki which includes extra "/" characters ... you'll find that if you call up http://www.wellho.net/share/the/pizza.html you'll be redirected to http://www.wellho.net/share/deep.html - and that will change in your browser bar too.
Example from .htaccess: RewriteRule (.*)/(.*)\.htm /share/deep.html [R=301,L]
The difference between permanent and temporary moves is that the browser remembers a permanent move and should ask for the new location on the following visit; but in the case of a temporary move it will remember the original setting and try that on the following occasion.
See [here] for status code listing (at wikiPedia).