Advanced 8 min read

How to Write Custom Clash Rules: Rule Types, Matching Order, and the MATCH Fallback

Learn how DOMAIN-SUFFIX, IP-CIDR, GEOIP, and other Clash rules work, why rules are evaluated from top to bottom, and how a misplaced MATCH fallback can break traffic routing.

Rule syntax: match conditions first, outbound target last

Clash routing rules go in the top-level rules: list in your configuration file. A typical rule looks like type,match value,outbound target. For example, DOMAIN,docs.example.com,DIRECT sends traffic directly when the destination domain is exactly docs.example.com. DIRECT and REJECT are built-in targets for direct connections and blocked traffic. You can also specify a proxy or policy group already defined in your configuration. When a rule points to a policy group, that group’s selection or scheduling logic determines the actual outbound route.

  1. Switch to Rule mode first. Global and Direct modes don’t route traffic through this rule list, so saving the YAML alone won’t change the active mode.
  2. When editing a local configuration, add list items under the existing rules: key; don’t create another top-level key with the same name. Start each item with -, keep the indentation consistent, and separate fields with commas.
  3. When editing a remote subscription, first check whether your client supports configuration overrides and prepending rules, and where saved changes will be inserted. Direct edits to the subscription cache may be overwritten on the next update. If overrides aren’t supported, copy the configuration locally to test your changes.

Rule types: choosing between domains, IP ranges, and locations

TypeExampleWhat it matches
DOMAINDOMAIN,docs.example.com,DIRECTMatches only this domain, not its subdomains.
DOMAIN-SUFFIXDOMAIN-SUFFIX,example.com,DIRECTMatches example.com and its subdomains, but not notexample.com.
DOMAIN-KEYWORDDOMAIN-KEYWORD,example,DIRECTMatches destinations with example anywhere in the domain name, which may include unrelated domains.
IP-CIDRIP-CIDR,192.168.1.0/24,DIRECT,no-resolveMatches the destination IPv4 range. This prefix covers 192.168.1.0 through 192.168.1.255.
IP-CIDR6IP-CIDR6,fd12:3456:789a::/48,DIRECT,no-resolveMatches a destination IPv6 prefix; an IPv4 range rule can’t be used in its place.
GEOIPGEOIP,CN,DIRECTMatches the destination IP using the geographic data loaded by the core. CN denotes addresses classified as being in China in that data.

Use hostnames for domain rules and IP ranges for IP rules

For a domain rule, enter only the hostname—not https://, a port, or a path. DOMAIN-SUFFIX already matches subdomains, so there’s no need to add *.. Use DOMAIN to match a single host; use a suffix rule to cover a domain and all its subdomains. A web page may also connect to separate hosts for sign-in, images, and APIs. Changing the rule for the main domain doesn’t mean those other connections will use the same outbound route.

no-resolve only prevents this rule from triggering DNS resolution

When a connection provides a domain name but no destination IP is available for matching, an IP rule without no-resolve may trigger a DNS lookup. With this option, the rule won’t actively resolve a domain just to check for a match. It can still match if the connection already has a destination IP or an earlier rule has resolved one. This doesn’t disable client DNS or guarantee that no lookup will occur when the connection is established. If you want GEOIP,CN,DIRECT to cover domain requests that haven’t been resolved yet, a GEOIP rule with no-resolve can’t do that on its own.

GEOIP checks the destination IP, not the domain suffix or the device’s location. The same domain may resolve to different CDN addresses with different DNS servers, leading to different results. To always apply a specific policy to a domain, put its domain rule before GEOIP.

Matching order: the first matching rule wins

The core checks rules from top to bottom. It won’t go back and replace a match just because a later rule is more specific. To make updates.example.com an exception, put its exact-domain rule before the suffix rule for example.com. Reverse those lines and the broader rule will match the update domain first.

This rules: snippet illustrates rule order; it isn’t a complete subscription. Proxy Select must be the name of a policy group already defined in your configuration. Replace it with the actual name before pasting, and adjust the example domains and IP ranges to suit your needs.

rules:
  - DOMAIN,updates.example.com,DIRECT
  - DOMAIN-SUFFIX,example.com,Proxy Select
  - IP-CIDR,192.168.1.0/24,DIRECT,no-resolve
  - GEOIP,CN,DIRECT
  - MATCH,Proxy Select
  • updates.example.com matches the first rule and connects directly; the second rule isn’t checked.
  • api.example.com matches the second rule and is routed through the policy group. Even if its resolved IP is in CN, the later GEOIP rule won’t override that decision.
  • A connection to 192.168.1.20 can match the third rule and connect directly without using a domain to identify that address. If you connect to a domain that points to this address and the destination IP isn’t available when the third rule is evaluated, the result may differ.
  • If none of the first four rules match, the connection is routed to the policy group specified by MATCH.

MATCH determines where unmatched connections go; it isn’t a retry rule for failed outbound connections. If the selected policy group has no available outbound, check its selection or fallback settings rather than adding another MATCH below it.

Where to put the MATCH fallback: decide what happens to unmatched traffic

MATCH takes no match value; write it as MATCH,outbound target and usually place it last in the active rule list. Put it first and it will bypass all other routing rules; put it in the middle and only rules above it can take effect. If you add two, the first catches any connection that reaches it, so the second never runs. This is an easy mistake when adding custom rules through a subscription override: if the subscription already ends with MATCH, rules appended after it can be saved but will never match traffic.

  • Proxy only the sites you specify: Add the domains or rule sets to proxy first, then finish with MATCH,DIRECT.
  • Connect directly only to specified destinations: Put direct-connection exceptions and any required LAN rules first, then send the remaining traffic to a proxy policy group.
  • Your subscription uses rule sets: When you see RULE-SET, check not only its position but also whether the referenced rule-providers loaded successfully. A rule set is evaluated where it’s referenced. Broad rule sets can match before your custom exceptions do.

Before choosing a fallback, decide what should happen to a new domain you haven’t seen before: should it connect directly or use a proxy? That determines the target for MATCH. When using multiple subscriptions, don’t copy a policy group name from an example without checking that it exists in each configuration.

Rules not working? Trace a connection through the active configuration

  1. Make sure the configuration is loaded. After saving, reapply the configuration and check which subscription is selected and which outbound mode is active. If the core refuses to load it, check YAML indentation, rule types, and target names, then confirm which core the client is running. Extensions supported by Clash Meta (mihomo) may not work with older Clash cores.
  2. Test with a new connection. If your client has a Connections page, close the relevant connection before sending a new request. Browsers may reuse existing connections, so an old connection shown after a page refresh doesn’t prove the new rule is working. Test one domain that should match a specific rule and another that should use the fallback.
  3. Check the matched rule separately from the outbound route. In your client’s connection details or logs, check the destination host, matched rule, and policy group. If the rule is right but the outbound route isn’t, check which node the group currently selects. If a broader suffix rule or rule set matched first, move the exception above it—you don’t need to change DNS first.
  4. Check whether the traffic reaches the core. If the destination request doesn’t appear in the connection list, check whether the app uses the system proxy or whether the current TUN route captures the connection. If the connection appears but shows only an IP address, check whether the app connects directly by IP and whether the core has obtained the domain name. An IP address alone can’t match a domain rule.

Change only one rule at a time while troubleshooting. First check its position in the list actually loaded by the core, then inspect the match result for a new connection. This helps you tell whether the rule is malformed, overridden by rule order, or never reached because the traffic isn’t entering Clash.

Download Clash