function hstda_rewrite_url($url, $domains, $domainAlias = NULL)
{
    $parts = parse_url($url);
    // Strip www. from url
    $parts['host'] = hstda_trim_url($parts['host'], 'www.');
    // Only rewrite local urls
    if (isset($parts['host']) && !in_array($parts['host'], $domains)) {
        return $url;
        // If the host is eg. twitter.com leave it unchanged
    } else {
        $parts['scheme'] = "https";
        $parts['host'] = isset($domainAlias) ? $domainAlias : htsda_get_domain_alias($parts['host']);
        // TODO Is there cases where we should also replace $parts['query'] ?
        return hstda_build_url($parts);
    }
}
function hstda_rewrite_url($url, $domains, $domainAlias = NULL)
{
    // $url must start with http
    // => don't touch relative or non http/https urls
    if (substr($url, 0, 4) != 'http') {
        return $url;
    }
    $parts = parse_url($url);
    // Strip www. from url
    $parts['host'] = hstda_trim_url($parts['host'], 'www.');
    // Only rewrite local urls
    if (isset($parts['host']) && !in_array($parts['host'], $domains)) {
        return $url;
        // If the host is eg. twitter.com leave it unchanged
    } else {
        $parts['scheme'] = 'https';
        $parts['host'] = isset($domainAlias) ? $domainAlias : htsda_get_domain_alias($parts['host']);
        // TODO Is there cases where we should also replace $parts['query'] ?
        return hstda_build_url($parts);
    }
}