Why is my conditional redirection not working?