Advanced 7 min read
Clash Custom Rules: Rule Types, Matching Order & Priority
Learn Clash rule syntax for DOMAIN-SUFFIX, IP-CIDR, RULE-SET, and MATCH, how rules are evaluated from top to bottom, and how no-resolve affects matching.
Rule Syntax: Types, Match Values, and Policies
In rule mode, the core checks incoming connections against the rules: list from top to bottom. In most cases, the first matching rule determines where a connection goes; the core does not evaluate every rule and then choose the most specific match. Examples below use mihomo (formerly Clash.Meta). Older Clash cores and client-specific override features may behave differently, so check the configuration that actually loads after saving.
For example, DOMAIN-SUFFIX,example.com,DIRECT has three fields: the rule type, the match value, and the target policy. DIRECT connects directly, while REJECT blocks the connection. You can also use the name of a policy group defined in your configuration, such as PROXY, but that is not a built-in policy—the name must match an existing group. MATCH takes no match value; specify only the type and target.
| Type | Example | What it matches |
|---|---|---|
DOMAIN | DOMAIN,api.example.com,DIRECT | Matches the exact host api.example.com, not its subdomains. |
DOMAIN-SUFFIX | DOMAIN-SUFFIX,example.com,DIRECT | Matches example.com and its subdomains, but not otherexample.com. |
DOMAIN-KEYWORD | DOMAIN-KEYWORD,example,REJECT | Matches connections whose domain contains this string, which can include unrelated domains. |
IP-CIDR | IP-CIDR,192.168.50.0/24,DIRECT,no-resolve | Matches the destination IPv4 range. For IPv6 destinations, use IP-CIDR6, for example 2001:db8::/32. |
RULE-SET | RULE-SET,office-domains,DIRECT | References the rule set named office-domains. |
MATCH | MATCH,DIRECT | Matches connections not caught by any preceding rule. |
For domain rules, enter a hostname—not https://, a port, or a URL path. To match the host in https://api.example.com:8443/v1, enter api.example.com. A domain rule cannot distinguish between /v1 and /v2 on the same host.
Rule Order: Put Specific Exceptions First
Suppose you want to reject only blocked.example.com while letting other hosts with the same suffix connect directly. Place the exact-match rule before the suffix rule. The snippet below shows only the rules, so you can focus on their order; there is no need to create a policy group.
rules:
- DOMAIN,blocked.example.com,REJECT
- DOMAIN-SUFFIX,example.com,DIRECT
- IP-CIDR,192.168.50.0/24,DIRECT,no-resolve
- MATCH,DIRECT
blocked.example.commatches the first line. Swap the first two lines, and it will match the suffix rule first, so the later exact-match rejection will never apply.api.example.commatches the second line.cdn.blocked.example.comalso matches the second line because the first line matches only one exact hostname. To reject the entire subdomain tree, change the first line to aDOMAIN-SUFFIXrule forblocked.example.com.- Connections with destination IP
192.168.50.12match the third line.192.168.51.12falls outside this/24range and reaches the last line. Both connections go direct, so check the matched rule to tell them apart.
no-resolve: Skip DNS lookups for this rule
Place no-resolve after the target policy. For the IP-CIDR example, it means that if the core has a destination domain but no destination IP available for matching, it will not initiate a DNS lookup just to evaluate this rule. The core continues to the next rule. If the destination is already an IP, or a usable destination IP is available by the time this rule is checked, the rule can still match.
Why can one parameter change where traffic goes?
The examples below use a test domain and a documentation-only IP range. Suppose the core receives the domain files.example.com and has no destination IP available when it reaches the first line. If it performs a lookup, assume the result is 192.168.50.12.
rules:
- IP-CIDR,192.168.50.0/24,DIRECT,no-resolve
- DOMAIN,files.example.com,REJECT
- MATCH,DIRECT
- Keep
no-resolve: the first line has no destination IP to check and does not initiate a lookup. The second line matches the exact domain and rejects the connection. - Remove
no-resolve: the first rule can trigger a lookup to check for a match. If the lookup returns an address in the range shown above, the connection goes direct on the first line and never reaches the second. - Keep
no-resolve, but use192.168.50.12as the destination: the first line already has an IP to check, so it can still match.
What changes is whether the first rule can obtain the information it needs to match—not the rule's priority. This parameter does not disable client DNS or guarantee that later rules or the eventual connection will avoid DNS lookups. If traffic to a domain must always be rejected as soon as that domain is identified, put the exact-domain rule before any IP-range rule that could catch it. That is more reliable than depending on whether a destination IP is available at that point.
RULE-SET: Define the Set, Then Choose Where to Reference It
As the number of domains grows, you can move the match entries into a rule set. rule-providers: defines where the content is loaded from; RULE-SET in rules: determines when the set is checked and which policy applies to a match. Defining a rule set does not automatically add it to traffic routing.
Defining a Local Domain Rule Set
This mihomo example uses a local YAML file. Paths are resolved relative to the core's working directory, which may differ from the directory containing your subscription file. If a GUI client manages the core, first check its working directory and which paths it can read.
rule-providers:
office-domains:
type: file
behavior: domain
format: yaml
path: ./rules/office-domains.yaml
rules:
- DOMAIN,blocked.example.com,REJECT
- RULE-SET,office-domains,DIRECT
- MATCH,DIRECT
The contents of office-domains.yaml are shown below. +.example.com matches the root domain and its subdomains; the exact rejection for blocked.example.com still takes effect first.
payload:
- '+.example.com'
- Use
behavior: domainfor domain match entries. Do not putDIRECTor a group name in the list. For a list containing only IP ranges, usebehavior: ipcidr. - To combine domain and IP rules in one list, use
behavior: classical. An entry can be written asDOMAIN-SUFFIX,example.com; specify the target policy on the outerRULE-SETreference line. - If a domain in a rule set needs an exception, put the exception before the rule-set reference; there is usually no need to edit the entire list. When troubleshooting, check the rule-set name, file path, and core loading logs.
Subscription Overrides and Troubleshooting: Check the Active Rules, Then Test Matches
A subscription update may regenerate the configuration. Direct edits to a subscription copy can be lost on the next update. If your client supports local rule overrides, use its built-in mechanism to save changes. If you use a separate configuration file, keep a backup and test again after switching the active configuration. To confirm an override worked, inspect the merged rules—not just the contents in the editor.
- Check the mode and traffic path. Make sure the client is in rule mode, not global or direct mode. With the system proxy enabled, confirm the test app respects the proxy settings. With TUN enabled, confirm the target traffic actually enters the client. Connections that bypass the core will not appear in its rule-match logs.
- Check the rules in their effective order. If the override feature only appends custom rules to the end of the list, check whether the subscription already contains
MATCH. Rules appended after a fallback cannot receive connections that the fallback has already routed. Move exceptions before the relevant general rules, then reload the configuration. - Test one new connection at a time. For example, test
api.example.comandblocked.example.comseparately, then compare the destination, matched rule, and policy in the connection details or logs. When testing with an IP and port, first confirm the destination address is in the expected range. Existing connections may keep the policy they started with, so close them and try again. - Separate rule issues from outbound connection issues. If the wrong rule matches, check rule order, domain availability, and IP resolution conditions. If the right policy matches but the connection still fails, check DNS, the destination port, the selected node, or direct routing. If tests through the system proxy and TUN behave differently, compare whether each record shows a domain or an IP as the destination. Do not rely on the toggle state alone.
After saving and updating the subscription once, retest three types of connection: a custom exception, a destination covered by a general rule, and one that should reach the fallback. Together, these checks show whether the override persisted, whether it was inserted in the right place, and whether MATCH is still last.