Refactor get_redirect_pattern, add graph depth limit to avoid infinite
loops on graph cycles