add a utility function to detect redirects and handle them