Regular expressions in filtering
DNS Management
Regular expressions (regex) provide a powerful pattern-matching tool within Pi-hole, enabling sophisticated domain filtering that goes beyond simple exact matches. Where standard blocklist entries match specific domains, regex rules can match multiple domains based on patterns.
Consider these common Pi-hole regex patterns:
^ads\..* # Blocks all domains starting with "ads."
.*\.example\.com # Blocks all subdomains of example.com
^(.+[-_.])??ads[.-].*$ # More comprehensive ad domain blocking
Managing Regex in Pi-hole:
- Through the web interface:
- Navigate to "Blacklist" under "Group Management"
- Select "Add regex filter" option
- Enter your pattern
- Save changes
Common Use Cases:
- Block all subdomains of a problematic domain
- Match common advertising domain patterns
- Filter domains containing specific keywords
- Block patterns that rotate domains (e.g., ad1.site.com, ad2.site.com)
A Word of Caution: Regex filters are powerful but require careful testing. An overly broad pattern might accidentally block legitimate domains. Always verify regex rules in the Query Log before implementing permanently.
Best Practice: Start with simple patterns and test thoroughly. Document your regex rules with comments explaining their purpose, as patterns can be difficult to decipher later.
Something here not working for you? Ask in the community — other makers and the Little Bird team read it.