Why am I getting a 404 error when trying to conditionally redirect to a different page?