Configure rules: put explicit routing decisions first
Back up your current configuration, then make sure the client is running in rule mode (mode: rule). Replace only the top-level rules section below, keeping your existing nodes, proxy groups, and DNS settings. Keep just one top-level rules: key in the YAML file. If your configuration comes from a remote subscription, use the client's supported override feature so a subscription update won't overwrite your local changes.
rules:
- IP-CIDR,192.168.0.0/16,DIRECT,no-resolve
- IP-CIDR,172.16.0.0/12,DIRECT,no-resolve
- IP-CIDR,10.0.0.0/8,DIRECT,no-resolve
- DOMAIN-SUFFIX,bilibili.com,DIRECT
- DOMAIN-SUFFIX,wikipedia.org,Proxy
- GEOIP,CN,DIRECT
- MATCH,Proxy
DIRECT is the built-in direct outbound. Proxy is an example policy group name, not a reserved keyword. Replace both instances of Proxy with a name that actually exists under proxy-groups, and make sure the group has the intended proxy node selected—not DIRECT. The spelling, capitalization, and spaces in the group name must match exactly. Reload the configuration when you're done; saving the file alone won't update the rules in the running core.
Rule order: the first match wins
The core checks connections in the order rules appear in rules; it won't move more specific rules to the top for you. For example, if the core knows the destination domain is www.bilibili.com, it stops at DOMAIN-SUFFIX,bilibili.com,DIRECT. Even if the resolved IP belongs to another region, the core won't go on to check GEOIP.
Put single-host exceptions before suffix rules
DOMAIN-SUFFIX,bilibili.com matches the root domain and its subdomains, but not notbilibili.com. To route a specific hostname differently, add a DOMAIN exception before the corresponding suffix rule. These two lines illustrate the priority order; adapt them to your needs:
- DOMAIN,api.bilibili.com,Proxy
- DOMAIN-SUFFIX,bilibili.com,DIRECT
If you swap these lines, api.bilibili.com will match the suffix rule first and go direct. Enter only the hostname as the rule value—no https://, port, or path. These rule types can't distinguish between two URL paths on the same domain.
Add domains based on connections, not page titles
A webpage may connect to separate hosts for the page, its login API, and image CDN. Routing the main page directly doesn't make its other connections inherit that route. To add a rule, find the misrouted hostname in the connection log, then decide whether to add a single DOMAIN rule or cover all its subdomains. Don't route an entire shared CDN suffix through the same outbound just to fix one API.
GEOIP and MATCH: use IP classification to catch remaining traffic
| Rule | What it checks | Where to place it |
|---|---|---|
IP-CIDR | Whether the destination IP falls within a specified range | Near the top, for known private network ranges |
DOMAIN-SUFFIX | The destination domain known to the core | After specific hostname exceptions and before GEOIP rules |
GEOIP,CN | How the core's geolocation data classifies the destination IP | After explicit domain-routing rules |
MATCH | All connections that haven't matched a rule yet | At the end of the list |
GEOIP,CN,DIRECT matches based on IP classification, not on whether a domain ends in .cn. If a connection has only a domain and none of the earlier rules match, the core may resolve it during routing to check its IP. The match can change if the domain points to a different CDN or the geolocation data is updated. For services that need a consistent outbound, use an earlier domain rule to make your intent explicit rather than relying on the resolved IP happening to fall in a particular country or region.
The example adds no-resolve to the end of each of the three private IPv4 range rules. This prevents the rule from resolving the destination domain solely to check whether its IP falls within the range; the rule can still match if a usable destination IP is already available. It doesn't disable DNS for the whole configuration or replace IPv6 range rules. If you access office services by internal domain name, these three rules alone can't guarantee a direct connection. Add direct rules for the relevant office domains before the GEOIP rule, and make sure the core can use the correct internal DNS.
MATCH,Proxy sends any connection that hasn't matched another rule to the proxy group, including domestic IPs not covered by the geolocation data. If you change it to MATCH,DIRECT, all unmatched connections go direct by default. That suits configurations that proxy only explicitly listed traffic, but it's a different routing policy. Don't put MATCH in the middle of the list: later rules won't get a chance to match, even if the configuration loads.
Split rules into rule-providers to keep lists out of the main config
When a domain list grows large enough to maintain separately, store it in a rule set and keep rule order and outbounds in the main config. This example uses a local YAML rule set in Mihomo. Put rule-providers at the top level, alongside rules. If that section already exists, add only the domestic-sites entry.
rule-providers:
domestic-sites:
type: file
behavior: classical
format: yaml
path: ./rules/domestic-sites.yaml
Resolve this relative path from the core's working directory; don't assume it's relative to the subscription file. Create rules/domestic-sites.yaml and put only match conditions under payload:
payload:
- DOMAIN-SUFFIX,bilibili.com
Replace DOMAIN-SUFFIX,bilibili.com,DIRECT with RULE-SET,domestic-sites,DIRECT. Specify the outbound on the line that references the rule set, not in payload. If a new proxy-domain rule overlaps with the direct-routing list, move the proxy exception above RULE-SET; otherwise, the direct rule set will match first.
This uses behavior: classical, so the list keeps the DOMAIN-SUFFIX,domain syntax. Don't just change behavior to domain and reuse the same list; the rule-set type and its contents must use matching syntax. If the core reports that it can't find the file, check the working directory and filename first. If it reports an unknown configuration field, confirm which core the client is actually using rather than assuming compatibility based on the client name.
Verify routing: check both the matched rule and the actual outbound
First, make sure the client is set to Rule mode. Then open the Connections page and check the destination, matched rule, and outbound chain. Re-establish the connection after each change; browsers may reuse existing connections, so refreshing a page doesn't always test the new rules. You can also send a test request through the local proxy port. Use 7890 below only if mixed-port or the HTTP proxy is actually listening on that port:
curl -x http://127.0.0.1:7890 -I --max-time 15 https://www.bilibili.com/
curl -x http://127.0.0.1:7890 -I --max-time 15 https://www.wikipedia.org/
- Test a direct-routed domain. The first request should match Bilibili's suffix rule. If you've split out the list, it should instead match the
domestic-sitesrule set, withDIRECTas the final outbound. - Test a proxied domain. The second request should match Wikipedia's suffix rule. If the rule matches but the final outbound is still
DIRECT, check the proxy group's current selection. - Test GEOIP matching and the fallback. Choose one service not in your domain list whose destination IP is known to be classified as
CNby the current geolocation data, and another service that doesn't match any earlier rule. They should matchGEOIPandMATCH, respectively. Check the connection log to confirm.