 
                 // Check the domain exceptions. If a domain exception matches, this filter does not apply.
                 for domainRegex in domainExceptions {
-                    let range = NSRange(location: 0, length: host.characters.count)
-                    if domainRegex.firstMatch(in: host, options: [], range: range) != nil {
+                    let range = NSRange(location: 0, length: documentHost.characters.count)
+                    if domainRegex.firstMatch(in: documentHost, options: [], range: range) != nil {
                         continue domainSearch
                     }
                 }

                    return try! NSRegularExpression(pattern: regex, options: [])
                }

                blockRules.append(BlockRule(regex: filterRegex, domainExceptions: domainExceptions))
            }
        }
    }

    func isBlocked(_ url: URL) -> Bool {
        guard let host = url.host else { return false }

        let absoluteString = url.absoluteString
        let range = NSRange(location: 0, length: absoluteString.characters.count)

        domainSearch: for rule in blockRules {
            // First, test the top-level filters to see if this URL might be blocked.
            if rule.regex.firstMatch(in: absoluteString, options: .anchored, range: range) != nil {
                // We matched, and there are no exceptions.
                guard let domainExceptions = rule.domainExceptions else {
                    return true

    }

    override func startLoading() {
        guard !ContentBlocker.blockList.isBlocked(request) else {
            let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: ["Content-Length": "0"])
            client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed)
            client?.urlProtocolDidFinishLoading(self)
            return
        }
