template_redirect is probably what you want.