more robust parsing and escaping of urls