Skip navigation.

Harold's Home

XML version of this site

PHP Scripts

CLI fun
Mail on 404
HB-NS (NewsScript)


APOD to Desktop
Dreamweaver Extensions


Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Sample Movies


Redirect 301
Drew McLellan writes about HTTP status codes on this post entitled 404 - Error Badly Assigned. I'll not reiterate my comment to his post but write about another wonderful thing HTTP status codes gives us, developers/maintainers of sites. This article assumes you're using Apache, though it will likely work on other server-platforms that conform to W3C standards.

For some reason the HTTP status messages have always fascinated me. Ever since I realised that the server I use at work allowed me to redirect users who had mistyped a url to a custom error page I was hooked. Later on I realised that this could benefit not only users (by providing a friendly error message (warning: mostly in Dutch)) but could also notify me by sending an e-mail message if certain conditions were met. I have even written a short article with sample PHP code about that.

Even later on I carefully read the specifications for HTTP status codes and discovered there was a code for a resource that was moved. Possibly because a document was moved from one directory to another. The code in question is 301, moved permanently. Here's how it works:
A user requests an outdated URL. The server looks it up but finds that the page has moved (see the code sample later on) and issues an HTTP 301 status code, telling the user's browser that the page has moved. It also tells the the user's browser what the correct url is and the browser should redirect to that location.
Here's how to implement it in your .htacesss file (*):
Redirect 301 /med_pix

Thats all on one line. It will tell the server to redirect all requests for the directory /med_pix to the directory /hvu-pix on the server
See it in action.
The system is so good in fact that a request for will redirect to

(*) Note: this will only work if your system administrator has enabled this override in the server configuration this is not always the case so mail your sysadmin if you're unable to get this to work.
You can also do this kind of stuff in your server config file but I assume most peope don't have access to that. Heck, I haven't got acccess to that on this server!

There's a lot more that can be done with the Apache Redirect directive, especially if you use RedirectMatch, which allows you to use regular expressions in redirects. Remind me to write about that another time.

Show all items | Read all items

About, copyright, privacy and accessibility | Mail