Ensure the user-originating redirection url is safe, i.e. within same scheme://domain:port

def get_safe_return_to(request, return_to):
    """
    Ensure the user-originating redirection url is safe, i.e. within same scheme://domain:port
    """
    if return_to and is_safe_url(url=return_to, host=request.get_host()) and return_to != request.build_absolute_uri():
        return return_to