Advanced Config 8 min read

Clash Custom Rules: DOMAIN-SUFFIX, IP-CIDR & Rule Priority

Learn Clash rule syntax, common rule types, first-match order, how no-resolve works, and how to manage large rule sets with rule-providers.

The rules field: put the match condition first, then the policy

rules is a top-level YAML list in the configuration file. A standard entry is usually written as “type,match value,policy.” MATCH needs no match value, so write it as MATCH,DIRECT. The hyphen before each list item is YAML syntax; commas separate the rule fields for the core. Indent with spaces and use standard ASCII commas between fields.

Use the snippet below as the rules section of a standalone test configuration. DIRECT and REJECT are built-in policies for direct connections and blocking, so the example does not rely on subscription nodes or proxy groups. The domains and IP ranges are illustrative and do not represent live test services.

mode: rule
rules:
  - DOMAIN,legacy.example.org,REJECT
  - DOMAIN-SUFFIX,example.org,DIRECT
  - IP-CIDR,192.0.2.0/24,REJECT,no-resolve
  - MATCH,DIRECT
Type Matches Example and notes
DOMAIN Exact hostname legacy.example.org does not include its subdomains.
DOMAIN-SUFFIX A domain and its subdomains example.org matches www.example.org, but not notexample.org.
IP-CIDR A destination IPv4 address or range 192.0.2.0/24 includes 192.0.2.25, but not 192.0.3.25. Use /32 to match a single IPv4 address.
MATCH All requests not matched by an earlier rule Put this last as the fallback policy.

For domain rules, enter only the hostname—not a scheme, port, path, or wildcard. To send matching traffic to a proxy group, replace DIRECT with the exact name of a group in your configuration, including its capitalization. When editing an existing configuration, add entries to its current rules list; do not create another top-level field with the same name.

Rule priority: put specific exceptions before broad rules

In rule mode, the core checks rules in list order and applies the policy of the first match. It does not compare a later, more specific rule against an earlier one. For legacy.example.org, the first rule in the example blocks the connection. Swap the first two rules, and the suffix rule matches first, so the connection goes DIRECT instead.

  1. Put individual exceptions first. If one hostname needs a different policy from other hosts in the same domain, place its DOMAIN rule before the DOMAIN-SUFFIX rule.
  2. Then add broader rules. Apply the same ordering to IP ranges: if 192.0.2.25/32 needs an exception, put it before 192.0.2.0/24. Prefix length does not determine rule priority automatically.
  3. Keep the fallback last. Before adding rules from a subscription, check where the existing MATCH rule sits. Rules added after it may still load, but they will never get a chance to match.

Rule order can also change the outcome when domain and IP rules overlap. If a request includes the hostname www.example.org, the suffix rule matches first. The core will not then check the later IP-range rule, even if the hostname resolves to an address in the blocked range. If an app connects directly to 192.0.2.25 and the core cannot identify a destination hostname, it skips the first two domain rules and reaches the IP-range rule.

no-resolve: don't resolve the destination hostname for this rule

IP-CIDR matches IP addresses. If the core has only a hostname, it may need to resolve it before it can tell whether the address matches an IP-range rule without no-resolve. Add no-resolve to prevent that rule from triggering a lookup just to check for a match. If no destination IP is available, the core continues to the next rule.

  • The app connects directly to an IP: The destination address is already known, so no-resolve does not prevent the IP-range rule from matching.
  • An earlier rule already obtained the destination IP: A later rule with no-resolve can still match against that address. The option does not discard an IP address already obtained.
  • Routing primarily by domain: Put explicit domain rules first. Add no-resolve to an IP-range rule only when you want it to check an address that is already available.

This option is not a global DNS switch, nor does it guarantee that the app or a later connection step will not perform a DNS lookup. When investigating an unexpected match, first check whether the core sees a hostname or an IP address. With TUN interception, for example, if the app resolves the hostname itself and the core cannot recover it, domain rules have no hostname to match. With fake-IP enabled, a virtual address in the connection log is not the real IP range of the remote server. Check the destination hostname, matched rule, and policy together—don't rely on the address column alone.

rule-providers: move large rule sets into separate files

In Mihomo, rule-providers declares rule sets, while RULE-SET in rules specifies where to use one and which policy to apply when it matches. Declaring a rule set without referencing it does not affect routing. The example below uses a local YAML file and requires no subscription server.

Declare and reference the rule set in the main config

mode: rule
rule-providers:
  blocked-hosts:
    type: file
    behavior: classical
    format: yaml
    path: ./ruleset/blocked-hosts.yaml

rules:
  - DOMAIN,allowed.legacy.example.org,DIRECT
  - RULE-SET,blocked-hosts,REJECT
  - DOMAIN-SUFFIX,example.org,DIRECT
  - MATCH,DIRECT

Add match conditions to the rule-set file

Place ruleset/blocked-hosts.yaml in the working directory actually used by the core. A GUI client may use a different working directory from the one where subscription files are stored. The entries below do not include policy fields such as REJECT; the policy is specified in the reference line in the main config.

payload:
  - DOMAIN-SUFFIX,legacy.example.org
  - DOMAIN,old-api.example.net

With this configuration, allowed.legacy.example.org matches the exact exception first and goes DIRECT, while api.legacy.example.org matches the rule set and is blocked. Move RULE-SET below the broader DOMAIN-SUFFIX,example.org,DIRECT rule, and the suffix rule will allow the latter subdomain first.

Use classical for entries with rule types like those above. domain and ipcidr are for domain lists and IP-range lists, respectively, and their file formats differ. For example, an ipcidr list should contain 192.0.2.0/24, not IP-CIDR,192.0.2.0/24. Before importing an existing rule set, check its behavior and file format; changing the file extension alone is not enough. Older Clash versions and different forked cores may support different fields. If a field is rejected, first check which core the client is actually running.

Rules not working? Check that the connection reaches the core, then review the match details

  1. Check which config is active. On the client's Config or Subscription page, confirm which item is enabled, save your changes, and reload the config. If the client supports overrides or placing rules before subscription rules, check the final rule order after each subscription update. Changes made to a local copy of a subscription may be overwritten by the next update.
  2. Check the mode and traffic path. Select Rule mode—not Global or Direct—in the mode switcher. Rule mode does not automatically route app traffic through the core; when using the system proxy, make sure the test app uses the system proxy settings. If the test connection does not appear in the core's records, check the app's proxy settings or whether TUN is active.
  3. Test both expected outcomes with fresh connections. Use your own test domain: choose one hostname that should match the exact exception and another that should match the suffix rule. Close existing connections or quit and reopen the test app before making new requests to avoid connection reuse. A browser error does not prove that REJECT matched; a direct connection can also fail because of DNS or an unavailable destination.
  4. Compare connection details with the logs. On the Connections page, check the destination, matched rule, and policy chain. A rejected request may not remain in the active connections list, so check the Logs page as well. If you see MATCH, check the hostname, destination IP, and whether the rule set loaded. If a broader rule earlier in the list matched, adjust the order and test again.

Save a working copy of your config before making changes. Change one thing at a time: the rule position, match value, or policy name. This makes it easier to tell whether a new rule was never checked, matched with the wrong policy, or matched correctly but the connection still failed—without resetting the subscription, DNS, and TUN settings all at once.

Download the latest version of Clash