protected function rewriteUrl($matches)
    {
        $matchedUrl = trim($matches['url']);

        // First check also matches protocol-relative urls like //example.com/images/bg.gif
        if ('/' === $matchedUrl[0] || false !== strpos($matchedUrl, '://') || 0 === strpos($matchedUrl, 'data:')) {
            return $matches[0];
        }

        $sourceUrl = dirname($this->file);

        if ('.' === $sourceUrl) {
            $sourceUrl = '/';
        }

        $path = $this->bundleUrl;

        if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
            // parse_url() does not work with protocol-relative urls
            list(, $url) = explode('//', $path, 2);
            list(, $path) = explode('/', $url, 2);
        }

        $bundleUrl = dirname($path);

        if ('.' === $bundleUrl) {
            $bundleUrl = '/';
        }

        $url = $this->rewriteRelative($matchedUrl, $sourceUrl, $bundleUrl);

        return str_replace($matchedUrl, $url, $matches[0]);
    }