Custom Clash Rules in Practice: Writing a Rules Config from Scratch
The rules bundled with your subscription are either too coarse or simply don't cover the app you care about. This article starts from match order, teaches you the common rule types, then uses rule sets to keep maintenance to a minimum.
Why the Rules in Your Subscription Aren't Enough
Most subscriptions ship with a set of routing rules covering the common cases — domestic direct connections, overseas proxying, ad blocking — which is enough for everyday use. But the moment your needs get even slightly specific — a domestic app that actually needs to go through the proxy, a game client that must connect directly, an ad domain you want to add yourself — you usually can't edit the subscription's built-in rules (they get overwritten on every update). That's when you need to add your own rules section to the config.
Most clients support managing subscription rules and custom rules separately (e.g. Clash Verge's "Override"/"Merge" feature). Write your custom rules into an override file, and subscription updates won't wipe them out. Check your client's docs for the exact steps — this article focuses only on how to write the rules themselves.
Understand Match Order Before You Start Writing Rules
rules is an array, and Clash compares each entry from top to bottom, using the outbound of the first rule it matches — every rule after that is never even checked. This means rule order is easier to get wrong than the content itself — put a broad rule near the top and a more precise rule below it will never get a chance to fire.
rules: # Matched top to bottom; stops at the first hit - DOMAIN-SUFFIX,ad.com,REJECT - DOMAIN-SUFFIX,google.com,Proxy - GEOIP,CN,DIRECT - MATCH,Proxy # Fallback, must be the last entry
A common mistake: putting GEOIP,CN,DIRECT (domestic direct) before your ad-blocking rules. If an ad domain happens to resolve to a domestic IP, it'll hit "domestic direct" before "reject," making the ad rule useless. When writing rules, always remember: the more specific a rule, and the more you want it to take priority, the further up it should go.
Common Rule Types in Practice
There are dozens of rule types, but the ones you'll actually need day to day are just the following, sorted by how often they're used:
-
Domain-based: DOMAIN-SUFFIX / DOMAIN-KEYWORD / DOMAIN
DOMAIN-SUFFIX,google.commatchesgoogle.comand all its subdomains — this is the most common form;DOMAIN-KEYWORDmatches domains that contain a given keyword, useful for "block/proxy anything that even touches this word," but watch out for false positives;DOMAINonly matches an exact domain, the most precise and least commonly used. -
GEOSITE: pre-packaged domain categories
GEOSITE,youtube,Proxyinstantly matches the entire batch of YouTube-related domains without writing them one by one. The rule-set community has already curated domain categories for almost every major site/app, which is far less effort than hand-writingDOMAIN-SUFFIXentries. -
GEOIP: match by the IP's country/region
GEOIP,CN,DIRECTis one of the standard fallback rules in almost every config file — any request that resolves to a domestic IP goes straight through direct connection, saving your proxy bandwidth. -
IP-CIDR: exact matching by IP rangeGood for local network ranges (like
192.168.0.0/16), fixed CDN IP ranges, and similar cases. Pairing it with theno-resolveparameter skips an unnecessary DNS lookup and speeds up matching.
rules: # Local network addresses go direct, no-resolve skips DNS lookup - IP-CIDR,192.168.0.0/16,DIRECT,no-resolve - IP-CIDR,127.0.0.0/8,DIRECT,no-resolve # Hit an entire domain category at once - GEOSITE,category-ads-all,REJECT - GEOSITE,netflix,Proxy # Fallback: domestic IPs go direct, everything else through the proxy - GEOIP,CN,DIRECT - MATCH,Proxy
Too Many Rules? Use RULE-SET to Pull in a Rule Set
If you need to block a few hundred ad domains, writing them all directly into the rules array makes your config file bloated and unreusable. This is where rule-providers (rule sets) come in — a large batch of rules lives in a separate file or subscription URL, and your config just needs one RULE-SET line to reference it.
rule-providers: reject: type: http behavior: domain url: "https://example.com/reject-list.txt" path: ./rule_providers/reject.yaml interval: 86400 rules: - RULE-SET,reject,REJECT - GEOIP,CN,DIRECT - MATCH,Proxy
behaviordetermines the content format of the rule-set file:domain(domain list),ipcidr(IP range list), orclassical(full rules, supporting almost every rule type) — each has its own format.intervalmakes the rule set auto-update periodically like a subscription, so you never have to manually edit your config when the ad-domain list gets updated.- Community-maintained rule sets (like ad-blocking lists or common streaming-domain lists) are usually ready to use as-is — no need to curate one from scratch.
For the detailed format of rule-set content itself (how each of the three behavior types is written), see Rule Provider Content in the docs section — we won't repeat it here.
In Practice: Building an "Ad Block + Domestic Direct + Specific App Proxy" Config
Combine the rule types covered above and you get a config skeleton that covers most people's everyday needs:
rules: # 1. LAN / local network addresses, direct, skip resolution - IP-CIDR,192.168.0.0/16,DIRECT,no-resolve - IP-CIDR,10.0.0.0/8,DIRECT,no-resolve # 2. Ad blocking via rule set, highest priority - RULE-SET,reject,REJECT # 3. Specific apps/sites that must go through the proxy (before the domestic rules!) - PROCESS-NAME,SomeApp.exe,Proxy - GEOSITE,netflix,Proxy - GEOSITE,youtube,Proxy # 4. Domestic domains/IPs, direct - GEOSITE,cn,DIRECT - GEOIP,CN,DIRECT # 5. Fallback: everything else through the proxy - MATCH,Proxy
Step 3 (routing specific apps/sites through the proxy) must come before step 4 (domestic direct). If the order is reversed, as soon as those domains resolve to an IP in a domestic range, step 4's GEOIP,CN,DIRECT will catch them first and the proxy rule will never get a chance to fire.
FAQ
Do I need to restart the client after changing rules?
Most clients support hot-reloading the config (usually called "Reload Config" or just "Reload") — click it after saving and the change takes effect, no need to restart the whole client. If a change doesn't seem to take effect, check that you edited the right file (subscription rules and override rules are stored separately).
Will having a lot of rules slow down the proxy?
Rule matching is a pure in-memory operation — matching against a few hundred rules typically takes microseconds, well below what you'd notice. What actually affects your experience is DNS resolution and the quality of the node itself, not the number of rules.
Why isn't the IP-CIDR rule I added taking effect?
The most common cause is ordering — a broader rule above it (like MATCH or a wider GEOIP range) is already catching the traffic first. Search for the IP/domain in the "Rules" page of the dashboard to see which rule it's actually hitting — that usually pinpoints the problem immediately.
Further Reading
Done reading? Give Clash a try
Grab the installer for any platform in one place — just follow the recommended tag and you can't go wrong. Up and running in minutes.