Take a look at http modules, you can use them to handle errors and redirections.