· Advanced · About 8 min read

Clash Rule Syntax Guide: DOMAIN, IP-CIDR, and Rule Order

Learn DOMAIN-SUFFIX, DOMAIN-KEYWORD, IP-CIDR, GEOIP, and RULE-SET syntax, how rules match from top to bottom, and where to place the fallback MATCH rule.

Rule order: exceptions first, MATCH last

First, select Rule mode in the client. Global and Direct modes do not route traffic through the rules below. In YAML, set the top-level option to mode: rule; rules go in the top-level rules: list. When editing an existing config, update that list rather than adding another top-level field with the same name.

Most rules use the format “type, condition, policy.” For the policy, use a built-in option such as DIRECT or REJECT, or enter the name of a defined proxy group. MATCH has no condition, so it takes just the type and policy, for example MATCH,DIRECT.

mode: rule
rules:
  - DOMAIN,login.example.com,DIRECT
  - DOMAIN-SUFFIX,example.com,REJECT
  - MATCH,DIRECT

These three lines show how to allow an exception for a login domain while rejecting other requests to the same domain. When the core sees login.example.com as the destination, the first line sends it direct. A request to img.example.com matches the second line instead. Swap the first two lines and the login domain will also match the second condition: the core checks rules from top to bottom and does not automatically move exact-domain rules to the top. Move MATCH to the first line, and the rules below it will never be reached.

Start with a few specific exceptions, then add broader rules. The policy on the final line determines how all remaining traffic is routed. If you use a proxy group, its name must exactly match the one defined in your config.

Domain rules: DOMAIN, DOMAIN-SUFFIX, and DOMAIN-KEYWORD

Domain rules match the destination hostname seen by the core, not the full URL in your browser’s address bar. Enter only the hostname—no https://, port, or /login path. None of these three rule types can distinguish between two page paths on the same site.

Rule Matches Does not match
DOMAIN,login.example.com,DIRECT login.example.com api.login.example.com
DOMAIN-SUFFIX,example.com,DIRECT example.com、img.example.com notexample.com、example.com.test
DOMAIN-KEYWORD,example,DIRECT example.com、notexample.com sample.com

Use DOMAIN for a single hostname. To cover both a root domain and its subdomains, use DOMAIN-SUFFIX and enter example.com without *.. DOMAIN-KEYWORD matches a character sequence anywhere in the hostname, so it can catch unrelated sites. When you know the exact domain suffix, prefer it over a broad keyword based on a brand name.

If an app connects directly to an IP address and the current config has no usable domain information for that connection, domain rules will not match. If a rule matches in your browser but not in an app, compare the destination fields for both connections first. Then decide whether to adjust the domain condition or add a separate rule for the destination IP.

IP-CIDR, IP-CIDR6, and GEOIP: match by destination address

IP-CIDR matches IPv4 destination ranges; IP-CIDR6 matches IPv6 destination ranges. An IPv4 /32 or IPv6 /128 represents a single address. For example, 192.168.50.0/24 covers 256 addresses, from .0 to .255. To route only 192.168.50.10 through a policy, use 192.168.50.10/32 so nearby devices are not included.

rules:
  - IP-CIDR,192.168.50.10/32,REJECT,no-resolve
  - IP-CIDR,192.168.50.0/24,DIRECT,no-resolve
  - IP-CIDR6,2001:db8::10/128,REJECT,no-resolve
  - GEOIP,CN,DIRECT
  - MATCH,DIRECT

This example uses different policies to show how ranges overlap. Traffic to 192.168.50.10 is rejected by the first rule; traffic to 192.168.50.11 in the same subnet matches the second. The IPv6 rule uses a documentation-only example address; replace it with the actual destination when deploying. These rules match destination addresses, not your phone’s address.

no-resolve controls DNS resolution for this rule only

With no-resolve, if the destination hostname is known but no destination IP is available, this rule will not trigger a lookup to check the address range or location. It can still compare a direct IP connection or a destination IP obtained before matching. This is not a global switch for client DNS, and it does not affect later IP rules that do not include the option.

GEOIP uses an IP database, not the domain suffix

GEOIP,CN,DIRECT checks whether the destination IP is classified as CN in the database used by the current core. It does not mean “send all .cn domains direct,” nor does it indicate a server’s physical location. A site using a CDN may resolve to different IPs on different networks. To keep a site on the same policy, put an explicit domain rule before the GEOIP rule. To investigate different classifications, note the actual destination IP and the database in use—not just the URL you visited.

RULE-SET: provider definitions, rule format, and placement

In cores that support rule providers, such as mihomo, rule-providers: defines the source of a rule set, which RULE-SET then references in rules:. Definition order does not set match priority; the position of each reference in rules: does. This lets you keep one or two local exceptions ahead of broader rule sets.

rule-providers:
  extra-block:
    type: file
    behavior: classical
    format: yaml
    path: ./ruleset/extra-block.yaml

rules:
  - DOMAIN,login.example.com,DIRECT
  - RULE-SET,extra-block,REJECT
  - MATCH,DIRECT

Save the rule set as a separate file named extra-block.yaml with the following contents. Relative paths are resolved from the core’s working directory, not necessarily from the main config file downloaded to your phone. In a graphical client, first check whether it lets you place local rule set files somewhere the core can read.

payload:
  - DOMAIN-SUFFIX,example.com
  - DOMAIN-SUFFIX,example.net

This example uses classical. Entries retain type fields such as DOMAIN-SUFFIX, but do not include REJECT; the calling line in the main config sets the policy for the entire set. domain and ipcidr behave differently: they use domain entries and IP range entries, respectively. Do not switch this file with type-prefixed entries to either format unchanged. If loading fails, check the path, behavior type, and indentation under payload:. For remote rule sets, also check the download status and confirm the response is actually a rule file.

Rules not working? Use a new connection to find the first match

Back up your current config before making changes. If the client supports local overrides, save custom rules there; edits to a subscription cache may be overwritten on the next refresh. After saving, reload the config and make sure the client is using the file you just changed.

  1. First, confirm the request reached the core. Look for the new request under Connections. If it is not listed, check whether the system proxy, VPN, or TUN is handling traffic from the app, and whether the app is excluded. Traffic capture and rule matching are separate steps. Reordering rules usually will not help if there is no connection record.
  2. Next, check which rule matched. Open the connection details or Logs in the client, then search by connection time, destination host, or destination IP. If the interface shows the matched rule and outbound chain, verify the rule first, then check which node the proxy group selected. If the rule matched correctly but the connection failed, investigate the outbound, destination service, or DNS instead of changing the rule right away.
  3. Test three destinations separately. Using the rule set above, if the core sees login.example.com, img.example.com, and www.example.org as the destinations, the expected matches are the exact-domain exception, the rule set, and MATCH, respectively. These are expected rule matches, not tests of whether the sites are reachable. For a real test, use destinations you can access or control while keeping the same three matching patterns.
  4. Change one thing at a time, then open a new connection. If a broad rule catches an exception first, move just that exception. Close the existing connection in the client’s connection list, if available, or quit the test app and send the request again to avoid reusing an old connection. Retest the same three destinations to make sure the exception, normal route, and fallback still work independently.
Download Clash