def fix_iis_url(server, url):
    """When a CGI application under IIS outputs a "Location" header with a url
    beginning with a forward slash, IIS tries to optimise the redirect by not
    returning any output from the original CGI script at all and instead just
    returning the new page in its place. Because of this, the browser does
    not know it is getting a different page than it requested. As a result,
    The address bar that appears in the browser window shows the wrong location
    and if the new page is in a different folder than the old one, any relative
    links on it will be broken.

    This function can be used to circumvent the IIS "optimization" of local
    redirects. If it is passed a location that begins with a forward slash it
    will return a URL constructed with the information in CGI environment.
    If it is passed a URL or any location that doens't begin with a forward slash
    it will return just argument unaltered.
    """
    if url[0] == "/":
        if server.getenv("HTTPS") == "on":
            dport = "443"
            prefix = "https://"
        else:
            dport = "80"
            prefix = "http://"
        prefix = prefix + server.getenv("HTTP_HOST")
        if server.getenv("SERVER_PORT") != dport:
            prefix = prefix + ":" + server.getenv("SERVER_PORT")
        return prefix + url
    return url