protected function rebuildURL($url)
    {
        $defaults = $this->config['defaultparts'];
        if (!preg_match('#^[a-zA-Z]+://#', $url)) {
            $url = $defaults['scheme'] . "://" . $url;
        }
        $parts = parse_url($url);
        if (!$parts) {
            //can't parse url, abort
            return "";
        }
        foreach ($parts as $part => $value) {
            if ($this->config['removeparts'][$part] === true) {
                unset($parts[$part]);
            }
        }
        //set defaults, if missing
        foreach ($defaults as $part => $default) {
            if (!isset($parts[$part])) {
                $parts[$part] = $default;
            }
        }

        return rtrim(http_build_url($defaults, $parts), "/");
    }