updating the redirect logic to avoid recursion