protected function setFallbackProtocol($pagePropertiesProtocol, $canonicalFallbackProtocol, $url)
    {
        $url = ltrim($url);
        if (empty($url)) {
            return null;
        }

        if (empty($canonicalFallbackProtocol)) {
            // Fallback not defined
            return $url;
        }

        if (!empty($pagePropertiesProtocol)) {
            // Protocol is well-defined via page properties (default is '0' with type string).
            // User cannot request with wrong protocol. Canonical URL cannot be wrong.
            return $url;
        }

        //get length of protocol substring
        $protocolLength = $this->getProtocolLength($url);
        if (is_null($protocolLength)) {
            //unknown protocol
            return $url;
        }

        //replace protocol prefix
        return substr_replace($url, $canonicalFallbackProtocol . '://', 0, $protocolLength);
    }