ClearURLsdocs
Specifications

Rule Catalogs

The ClearURLs rule catalogs and their JSON format.

At the moment ClearURLs knows three types of rule catalogs – each in its respective file. We will only look at two of these three catalogs, as the first version is already outdated.

Important

If you want to use one of these catalogs in your project and always retrieve the current version, please use the GitLab/GitHub Pages URL to access the catalogs to preserve the GitLab/GitHub infrastructure. The catalog files are available at:

Important

Do not use the older data.json catalog – it is outdated and will no longer receive any updates. Instead, use the data.min.json catalog!

data.min.json Catalog

The data.min.json catalog is a typical JSON file and saves all rules, exceptions, and redirections, that are maintained by the ClearURLs developers and the community. It is structured in providers. Providers are a logical unit under which multiple rules are applied under the same urlPattern. Examples of providers are companies or services like Google or Reddit. Every provider has the parent element providers, it holds the reference to all providers.

Every provider has the following properties:

  • urlPattern (required)
  • completeProvider (optional)
  • rules (optional)
  • rawRules (optional)
  • referralMarketing (optional)
  • exceptions (optional)
  • redirections (optional)
  • redirectRules (optional)
  • forceRedirection (optional)

Example of a data.min.json file

data.min.json
{
  "providers": {
    "provider name": {
      "urlPattern": "^https?://(?:[a-z0-9-]+\\.)*?domainName\\.com",
      "rules": ["trackingField"],
      "rawRules": ["/ref=[^/|?]*"],
      "referralMarketing": ["tag"],
      "exceptions": ["^https?://(?:[a-z0-9-]+\\.)*?domainName\\.com/re.*/redirector.html/"],
      "redirections": ["^https?://(?:[a-z0-9-]+\\.)*?domainName\\.com.*url\\?.*url=([^&]+)"],
      "redirectRules": [
        {
          "id": "outbound-target",
          "match": { "regex": "[?&]target=([^&#]+)" },
          "preprocessors": [{ "type": "urlDecode", "inputs": [1] }]
        }
      ]
    }
  }
}

urlPattern

The urlPattern is a regular expression, that must match every URL that should be affected by the specified rules, exceptions, or redirections of the provider. The typical structure is <protocol><subdomain><domain name><TLD><directorie>?<fields>.

In most cases the following expression covers all the needs, you only have to substitute the remaining <> fields: ^https?://(?:[a-z0-9-]+\\.)*?<domain name>\\.<TLD>. If you want to match with every TLD, you can substitute the TLD field with (?:[a-z]{2,}){1,}.

completeProvider

The completeProvider is a boolean, that determines if every URL that matches the urlPattern will be blocked. If you want to specify rules, exceptions, and/or redirections, the value of completeProvider must not be enabled. If the property is omitted, it is treated as not enabled.

rules

The rules property is a JSON array. Every element in this array will be automatically rewritten to (?:&amp;|[/?#&])(?:<field name>=[^&]*) to match the field name. The field name can still be a regular expression, but don't have to. If ClearURLs found a field that matches a rule in a given URL the field will be deleted. Only URLs that match the url Pattern will be checked for matching fields.

rawRules

Because other elements in a URL that should be filtered, besides "well-formed fields" aka normal rules, there is also the rawRules property. Regular expressions formulated in this property can refer to the entire URL and not just to individual fields. They are therefore applied directly to the URL.

referralMarketing

Since version 1.9.0, you can allow referral marketing in ClearURLs. Previously these fields were always removed. Since some people want to support other people through referral marketing, referral marketing can be allowed in the settings. If referral marketing is allowed, the regular expressions in this array are no longer filtered.

Attention

Referral Marketing is disabled by default.

exceptions

The exceptions property is also a JSON array. Every element in this array is a regular expression, that matches a URL. If ClearURLs found a URL, that matches an exception, no further processing on this URL is done.

redirections

The redirections property is an array of regular expressions that match URLs. The first regular expression group supplies the redirect target. The legacy decoder repeatedly percent-decodes the whole capture, as in ClearURLs 1.27.3. Use redirectRules to control the number of decoding steps or to preserve escapes in the target.

redirectRules

ClearURLs 1.28.0 supports redirectRules as an optional array on each provider in data.min.json. Use it to decode captures or build a target from several captures. The same provider can also define redirections. ClearURLs 1.27.3 reads the legacy redirections array and ignores redirectRules.

FieldMeaning
idRequired. Use a stable ID unique within the provider, with lowercase ASCII letters, digits, hyphens or underscores.
match.regexRequired. A regular expression applied to the original redirect URL after the provider matches. Use capturing groups for values needed by the target.
match.caseSensitiveDefaults to true. Set it to false to match case-insensitively, as legacy redirections do.
preprocessorsOptional ordered list of up to 16 capture transformations. Omit it to use the captures as matched.
replacePatternOptional target template containing literal text and numbered references such as §1§ and §2§. Defaults to §1§.
defaultSchemeOptional http or https. Applied after the template to complete a host with an optional path or a scheme-relative URL. A valid HTTP(S) scheme already present in the target is retained.

Each preprocessor requires a type and inputs. Capture numbering starts at 1. Use an array of distinct positive capture numbers or "all" to select every capture group. Only participating groups are transformed. An empty participating group expands to an empty string. A missing group referenced by the template or an input number beyond the regex's groups makes the match unusable.

PreprocessorOperation
urlDecodeApplies decodeURIComponent() and preserves literal +.
formDecodeReplaces literal + with spaces and then decodes percent escapes.
base64DecodeDecodes canonical standard Base64 to UTF-8 text.
base64UrlDecodeDecodes Base64url to UTF-8 text, with or without padding.
urlEncodeApplies encodeURIComponent(), for example before inserting data into a query value.
base64EncodeEncodes UTF-8 text as standard Base64.

ClearURLs applies the preprocessors in order, fills the template, and then applies defaultScheme if needed. The final target must be an absolute HTTP(S) URL. Raw control characters in captures or produced by a preprocessor invalidate the match. ClearURLs uses the validated target string as assembled, including its remaining percent escapes.

ClearURLs compiles the rules when loading the provider. Invalid configurations, including unknown fields or operations and invalid regexes, are skipped. For a repeated explicit ID, ClearURLs keeps the first successfully compiled rule. Explicit rules with distinct IDs can share the same regex. A successfully compiled explicit rule suppresses a legacy entry with the same regex string, even if the explicit rule fails to produce a target.

At runtime, ClearURLs tries the explicit rules in array order, then the eligible legacy rules. The first match that yields a valid target wins. If a match fails to produce a target, the next explicit rule can use the same regex with a different ID.

Examples

This provider excerpt shows a percent-encoded target with a literal fallback, a form-encoded search term, and a Base64url value assembled into a URL with two captures.

Redirect fields within a provider
{
  "redirectRules": [
    {
      "id": "outbound-target",
      "match": { "regex": "[?&]target=([^&#]+)" },
      "preprocessors": [{ "type": "urlDecode", "inputs": [1] }]
    },
    {
      "id": "outbound-literal",
      "match": { "regex": "[?&]target=([^&#]+)" }
    },
    {
      "id": "shop-search",
      "match": { "regex": "[?&]shop=([a-z0-9.-]+)&q=([^&#]+)" },
      "preprocessors": [
        { "type": "formDecode", "inputs": [2] },
        { "type": "urlEncode", "inputs": [2] }
      ],
      "replacePattern": "§1§/search?q=§2§",
      "defaultScheme": "https"
    },
    {
      "id": "document-view",
      "match": { "regex": "[?&]doc=([a-z0-9-]+)&view=([^&#]+)" },
      "preprocessors": [
        { "type": "base64UrlDecode", "inputs": [2] },
        { "type": "urlEncode", "inputs": [2] }
      ],
      "replacePattern": "https://docs.example/§1§?view=§2§"
    }
  ],
  "redirections": ["[?&]legacy=([^&#]+)"]
}
Query on the redirect URLExact target
?target=https%3A%2F%2Fshop.example%2Fa%252Fbhttps://shop.example/a%2Fb
?target=https://shop.example/a%FFhttps://shop.example/a%FF
?shop=shop.example&q=tea+biscuits%2Bmorehttps://shop.example/search?q=tea%20biscuits%2Bmore
?doc=abc&view=ZGV0YWlsICYgbm90ZXMhttps://docs.example/abc?view=detail%20%26%20notes
?target=https%3A%2F%2F%5Binvalid%5D%2F&legacy=https%3A%2F%2Fshop.example%2Fhomehttps://shop.example/home

In the second example, the percent decoder cannot decode the legacy byte %FF, so outbound-literal uses the capture as the target. In the last example, both matching explicit rules fail and the distinct legacy rule supplies the target.

forceRedirection

Some websites, such as Google, have manipulated URLs (<a> tags) in such a way that the URL is no longer called normally, rather via a built-in script. The result is that a simple redirect of the URL does no longer work. Thus, to still forward the URL by ClearURLs, there is the property forceRedirection which writes the URL into the main_frame object of the browser.

rules.min.hash

The rules.min.hash file saves a SHA256 hash of the data.min.json file and will be automatically generated by the GitLab CI runner. It is used to ensure, that the rules are not faulty after the download.

Important

Note that the hash must be in lowercase.

data.minify.json Catalog

The data.minify.json file is automatically generated by the GitLab CI runner from the data.min.json file and also the corresponding rules.minify.hash file. The data.minify.json is a minimal version of the data.min.json file where all line breaks and spaces, as well as default values and empty lists, are removed to save bandwidth.

On this page