· Advanced · 9 min read

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

Learn Clash rule syntax, when to use each rule type, how first-match ordering works, and where to place no-resolve and the MATCH fallback.

Rule types: Match by domain when you know the hostname

Use DOMAIN to route a single host differently, DOMAIN-SUFFIX to apply the same policy to a domain and all its subdomains, and IP-CIDR when you need to match a fixed address range. A website can change IPs or share an IP with other sites. For website-based routing, domain rules are usually more reliable than addresses copied from connection logs.

The examples below use Mihomo syntax (formerly Clash.Meta). rules: is a YAML list, with one comma-separated rule per item. A standard rule lists the match type, match value, and outbound policy in that order. When supported, optional parameters go after the policy. DIRECT sends traffic directly; REJECT blocks it. If you use a proxy group, its name must match the name under proxy-groups in your current config.

Type Example match value What it matches
DOMAIN api.example.com Matches this hostname only; subdomains are not included.
DOMAIN-SUFFIX example.com Matches the domain itself and subdomains at any level, such as v2.api.example.com; does not match notexample.com.
IP-CIDR 192.0.2.0/24 Matches destination IPv4 addresses from 192.0.2.0 through 192.0.2.255.
IP-CIDR6 2001:db8:10::/48 Matches destination IPv6 addresses with the same first 48 bits.

When copying an address from a browser, use only the hostname. For example, write https://api.example.com:8443/v1/status as DOMAIN,api.example.com,DIRECT. Matching uses the hostname, not the port—8443 and 443 are treated the same—or the path, so /v1/status is no different from any other path. To match a single IPv4 address, use /32; for a single IPv6 address, use /128.

Rule priority: put exceptions first and MATCH last

In rule mode, the core checks entries in rules from top to bottom and applies the first match. DOMAIN does not automatically precede DOMAIN-SUFFIX just because it is more specific, and /32 does not automatically take priority over /24. For an exception to take effect, place it above any broader rule that could also match.

rules:
  - DOMAIN,api.example.com,DIRECT
  - DOMAIN-SUFFIX,example.com,PROXY
  - IP-CIDR,192.0.2.10/32,REJECT,no-resolve
  - IP-CIDR,192.0.2.0/24,DIRECT,no-resolve
  - IP-CIDR6,2001:db8:10::/48,DIRECT,no-resolve
  - MATCH,PROXY

This config snippet illustrates rule order. PROXY represents a proxy group that already exists in the example config; replace it with your own group name. The example domains and IP ranges are for documentation only and cannot be used to check whether a service is online. To trace the rules one by one, first determine whether the core receives a hostname or an IP address:

  • When the core receives api.example.com, the first rule matches and sends it DIRECT. Move the second rule above the first, and the same host will go through PROXY instead.
  • When the core receives www.example.com, the second rule matches. Even if the domain later resolves to 192.0.2.10, the IP rule below it cannot override the policy already selected.
  • With only the destination address 192.0.2.10 available, the third rule sends it to REJECT. 192.0.2.11 skips the single-address exception and is sent direct by the fourth rule.
  • When the destination address is 2001:db8:10::8, the fifth rule matches. 198.51.100.8 is outside all the example ranges, so it falls through to MATCH,PROXY.

no-resolve: keep IP rules from triggering DNS lookups

no-resolve is an optional parameter for IP-based rules. When the core checks a rule with this parameter, if the connection has only a domain name and no destination IP available for matching, the rule will not trigger an additional DNS lookup just to check the IP range. If the destination IP is already known—for example, the app connects to an IP literal, or an earlier rule check has resolved the address—the rule can still match by IP.

Match only known addresses: keep no-resolve

For example, keep no-resolve if you want a direct-connection rule to apply only to devices connecting straight to a fixed IP address. This prevents the rule list from looking up an address just because it encounters that IP range rule while processing an ordinary domain name. The parameter affects rule matching only; DNS may still be needed to establish a connection, and it does not change the DNS server or proxy group.

Route by the domain's resolved IP: allow this rule to resolve

If you want a domain to go direct when it resolves to a particular IP range, and the core receives only the hostname, do not add no-resolve to the IP rule responsible for that check. Keep specific domain exceptions first, then place the IP rule before the fallback. A domain behind a CDN may resolve to different addresses depending on the resolver, network, or time. If the policy should follow the website rather than an IP range, use a domain rule instead.

When checking two adjacent IP rules, also check whether the first rule allows DNS resolution. It may resolve the address but fail to match its range; the next rule can then match the already-resolved address, even if it has no-resolve. Troubleshoot the entire rule list, not just the end of the rule that matched.

Merging subscription rules: check the final active rule list

Updating a subscription regenerates the config, so local edits to the subscription copy may be lost the next time it updates. If your client supports subscription overrides or rule extensions, use that feature to save custom entries. Clients differ in how they name and implement prepend, append, and replace options. After editing, inspect the active config and confirm the actual order under the top-level rules:.

  1. Check the mode first. Select Rule mode in the client, or confirm that the active config contains mode: rule. In Global or Direct mode, changing this rule list will not let you verify the expected routing order.
  2. Then check where the rule is inserted. Suppose your subscription includes RULE-SET,work,PROXY, and work is defined under rule-providers and can match api.example.com. To send that host direct, place DOMAIN,api.example.com,DIRECT before the RULE-SET entry. A rule set is evaluated where its reference appears in the list.
  3. Check the policy name. For example, if the subscription’s group is named “Manual Select,” use Manual Select as the third field in the rule. Matching a group only selects that group; the final route also depends on which outbound is currently selected within it.
  4. Check again after updates. Save and reload, then check for parse errors and verify the final rule order. Refresh the subscription manually and confirm that your custom rules remain in the intended position and that the referenced proxy groups still exist.

Rules not working? Use connection logs to find the problem

Choose two reachable destinations: one that should match your custom rule and another that should keep its existing policy. Reload the config, start new connections from the app, then check the destination hostname, destination IP, matched rule, and outbound chain on the client's Connections page. Browsers and apps may reuse existing connections; close the relevant connection or restart the app before retrying if needed. A page loading successfully only confirms that the request worked—it does not tell you whether it went direct or through a proxy.

  • No connection entry: First, confirm that the request is reaching the core. If the active config sets mixed-port to 7890, apps configured to use a local proxy should connect to 127.0.0.1:7890; if the config uses a different port, use that value instead. Some apps do not use the system proxy, so checking the system proxy toggle alone is not enough.
  • The log shows only an IP: This often happens when an app resolves the domain before opening a connection. With TUN enabled, the core may also receive only a connection to an IP address. Whether it can recover the hostname depends on DNS mapping, protocol sniffing, and the traffic itself. Check the destination details in the log first, then decide whether to fix hostname detection or use a suitable IP range rule.
  • The right rule matched, but traffic took the wrong route: Check the outbound chain and confirm that the policy group selected the expected proxy—or that it did not select DIRECT within the group. If the main page routes correctly but images or login requests do not, check the actual hostname for each request; one page can connect to multiple domains.

If the log shows a broad rule matching first, move the exception higher. If the connection log lacks the domain or address required by a rule, fix the match information first. Change one thing at a time, then open a new connection to the same destination to identify what made a difference. For details on where to switch modes and how they work, see the Getting Started guide to outbound modes.

Download Clash