Clash Custom Rules: Syntax and Matching Order, from DOMAIN-SUFFIX to MATCH
Learn how DOMAIN, IP-CIDR, PROCESS-NAME, and RULE-SET rules work—and why Clash applies the first matching rule from top to bottom.
Rule Matching Order: First Match Wins
In FlClash, first confirm that the current profile is enabled and the mode is set to “Rule.” Global and Direct modes don’t route traffic through the rules list below. This guide covers mihomo (Clash Meta); older Clash cores support some rule types and configuration fields differently.
The core checks rules from top to bottom and applies the first matching rule. An exact domain won’t automatically take precedence over a suffix rule, and a smaller subnet won’t automatically beat a larger one. To make an exception to a broad rule, put the exception above it.
mode: rule
rules:
- 'DOMAIN,login.example.com,DIRECT'
- 'DOMAIN-SUFFIX,example.com,PROXY'
- 'IP-CIDR,192.168.0.0/16,DIRECT,no-resolve'
- 'MATCH,DIRECT'
This is a configuration snippet. PROXY refers to a policy group with that exact name in your current profile. If your subscription uses the group name “Node Selection,” use “Node Selection” here. DIRECT is the built-in direct policy, and REJECT is the built-in reject policy. When editing an existing profile, merge these entries into its existing rules list; don’t add a second key with the same name.
login.example.commatches the first rule and goes direct. Swap the first two rules, and the same hostname will match the suffix rule first.api.example.comskips the first rule, matches the second, and is handed to thePROXYgroup. Which node it uses next depends on the group’s current selection.- When you visit the IP address
192.168.1.20, the first two domain rules don’t match, so the subnet rule handles the connection. The finalMATCHrule catches anything that hasn’t matched earlier.
Domain Rules: DOMAIN and DOMAIN-SUFFIX
DOMAIN matches an exact hostname. DOMAIN,example.com,DIRECT matches only example.com, not www.example.com. DOMAIN-SUFFIX,example.com,PROXY matches the root domain and dot-separated subdomains, including a.b.example.com, but not notexample.com. To cover subdomains, use the suffix directly—there’s no need to write *.example.com.
Rules take a hostname, not a full URL. For https://api.example.com:8443/v1/status, the domain rule should contain only api.example.com. Paths, query parameters, and ports aren’t part of the domain field. If you need to route by destination port, mihomo supports DST-PORT; however, a standalone port rule matches every connection using that port, not just this domain.
IP-CIDR: Subnets, Single Addresses, and no-resolve
IP-CIDR matches IPv4 destination addresses; use IP-CIDR6 for IPv6. For example, 192.168.0.0/16 covers 192.168.0.0 through 192.168.255.255, while 192.168.1.20/32 covers a single IPv4 address. A single IPv6 address uses /128. To reject one LAN address while allowing the rest to connect directly, add the rules in this order:
rules:
- 'IP-CIDR,192.168.1.20/32,REJECT,no-resolve'
- 'IP-CIDR,192.168.0.0/16,DIRECT,no-resolve'
- 'MATCH,PROXY'
no-resolve means this rule won’t actively resolve a hostname just to check for a match; it doesn’t disable DNS for the entire client. If the connection already has a destination IP, the rule can still compare it. If there’s only a hostname and no destination IP is available at the time, the rule won’t actively resolve one. Remove this option only if you want to route based on the IP returned by DNS, and verify which result the connection actually used.
Put explicit domain exceptions before subnet rules that may trigger resolution. This makes it easier to tell apart two issues: a domain rule in the wrong order, or a destination address that changes with DNS results. When troubleshooting, first test a numeric IP to confirm the subnet range, then test the actual domain to check resolution and rule matching. Don’t combine both tests into one.
PROCESS-NAME: Route Traffic by the Process That Opens the Connection
PROCESS-NAME matches the process name detected by the core—not the window title, install directory, or app store listing. On Windows, check the executable name under Task Manager → Details, then use the connection details to confirm which process opened the connection. Here’s where to put a process rule for curl.exe:
rules:
- 'DOMAIN,login.example.com,DIRECT'
- 'PROCESS-NAME,curl.exe,PROXY'
- 'MATCH,DIRECT'
With this order, connections from curl.exe to the login domain still match the direct rule first. Other connections from that process are sent to the proxy group only if the core can identify the process. If the process should take priority over domain exceptions, move its rule above the domain rules. An app may make requests through a background service, updater, or helper process. If a rule doesn’t match, check the actual process instead of adding more product names.
Process matching depends on the platform and traffic entry point providing process information. Check whether find-process-mode is set to off in the active configuration, and confirm that the traffic originates from an app on this device. Requests forwarded over a LAN can’t reveal the process name on another device from the destination address alone. Enabling TUN changes how traffic enters the core, but doesn’t guarantee process information is available for every connection.
RULE-SET: Rule Set Order and Policies
RULE-SET checks a defined rule set at its position in the list. A rule set has no priority outside rules: place it after domain exceptions to check those first. Put it after MATCH, and it won’t see connections already handled by the catch-all rule.
This example uses the classical type to put exact domains and domain suffixes in one local file. Place the file in a configuration working directory that mihomo can read, then declare it in the active profile. Relative paths are resolved from the core’s working directory—don’t assume that’s your browser’s download folder.
Active Profile: Declare and Reference the Rule Set
rule-providers:
work-sites:
type: file
behavior: classical
format: yaml
path: ./rules/work-sites.yaml
rules:
- 'DOMAIN,login.example.com,DIRECT'
- 'RULE-SET,work-sites,PROXY'
- 'MATCH,DIRECT'
Rule Set File: Define Match Conditions Only
payload:
- 'DOMAIN,docs.example.net'
- 'DOMAIN-SUFFIX,example.com'
Entries in payload don’t include a PROXY policy. In this example, the policy is set on the reference line: RULE-SET,work-sites,PROXY. You can apply a different policy to the same rule set by changing the reference line. Before adding an existing external rule set, check its data format and choose the matching behavior: use domain for a list of domains, ipcidr for a list of IP ranges, and classical for entries with rule type prefixes, like the ones above. A YAML file parsing successfully doesn’t mean its entries match the declared type.
Verify Rules: Check the Match, Then the Route
In “Profiles,” confirm that the modified profile is active, then reload it and check new requests under “Connections.” The exact menus may differ by platform; what matters is verifying the destination, matched rule, and actual outbound route—not just whether the client says it’s connected. Keep a recoverable copy of the profile before editing. If you edit a local copy of a remote subscription, also check whether the next subscription update will overwrite your changes.
- Make sure the request reaches the core. Test through the mixed port in your current profile. For example, if the port is set to
7890, runcurl -v -x http://127.0.0.1:7890 https://example.com/. Replace it with your actual port if it’s different. This test explicitly uses an HTTP proxy to isolate rule behavior; it doesn’t mean system proxy or TUN is routing traffic from other apps. - Make sure the change applies to a new connection. Disconnect existing connections from the list, or close the test app and make a new request. Browsers may reuse existing connections, so refreshing a page doesn’t always create a new connection you can use to verify the change.
- Use the result to narrow down the issue. If the connection matches
MATCH, check whether its recorded hostname, destination IP, and process name meet the conditions of your intended rule. If it matches a broad suffix or rule set, make sure the exception is above it. If the expected rule matches but access still fails, check the policy group’s selected option and the connection error instead of changing rule order.
Keep an explicit MATCH rule at the end, and test one exception, one regular match, and one catch-all target after each change. Record “destination → matched rule → outbound policy” so you can quickly tell whether a change after the next subscription update came from the rules, policy group, or traffic entry point.