如何在邮件服务器中创建过滤器匹配特定域名的HTTP/HTTPS URL
Alright, let's figure out how to build that email filter you need. You want to catch any emails with URLs starting with http/https that point to any subdomain of domain.org (or the main domain itself), even when those URLs get split across lines with a trailing equals sign. Here's a solid regex solution and how to implement it across common email servers:
This regex handles both full URLs and split URLs with trailing equals signs:
\bhttps?://(?:[^\s=]|=\s)*\.domain\.org(?:[^\s=]|=\s)*
Breakdown of the regex:
\b: Word boundary to avoid matching partial strings likexhttps://https?://: Matches bothhttp://andhttps://protocols(?:[^\s=]|=\s)*: Non-capturing group that handles:[^\s=]: Any character that's not a whitespace or equals sign (normal URL characters)=\s: An equals sign followed by any whitespace (including newlines) — this covers the line-split scenario*: Repeats the group zero or more times to handle multiple splits
\.domain\.org: Exact match for the target domain (dots are escaped since they're special regex characters)- The final
(?:[^\s=]|=\s)*: Matches any path/query parameters after the domain, even if they're split across lines
Here's how to use this regex in popular email systems:
Gmail Filters
- Go to Gmail's Filters and Blocked Addresses page
- Click Create a new filter
- In the Has the words field, paste the regex
- Check the box for Also apply filter to matching conversations (if needed)
- Click Create filter, then select your desired action (e.g., "Delete it", "Apply label")
- Important: Enable regex support by checking the box labeled Search using regular expressions before saving
Postfix (Body/Header Checks)
Add this line to your Postfix body checks file (usually /etc/postfix/body_checks):
/^\bhttps?://(?:[^\s=]|=\s)*\.domain\.org(?:[^\s=]|=\s)*$/ REJECT
- Replace
REJECTwith your desired action (e.g.,DISCARD,REDIRECT your-archive@example.com) - Reload Postfix to apply changes:
sudo systemctl reload postfix
Exchange Server
Use PowerShell to create a transport rule (the GUI may not support complex regex fully):
New-TransportRule -Name "Flag domain.org URLs" -SubjectOrBodyMatchesPatterns '\bhttps?://(?:[^\s=]|=\s)*\.domain\.org(?:[^\s=]|=\s)*' -ApplyHtmlLabel "Domain.org URL Detected"
- Adjust the action (
-ApplyHtmlLabel,-DeleteMessage, etc.) to fit your needs
- Test with split URL examples to ensure coverage:
- Split once:
https://site1.domain.or=\ng/path?query=123 - Split multiple times:
https://site1.do=\nmai=\nn.org/download - Full URL:
http://domain.org
- Split once:
- If your email server's regex engine doesn't recognize
\sas newlines, replace\swith[\r\n\t ]to explicitly match line breaks, tabs, and spaces - To avoid matching domains like
fake.domain.org.evil.com, modify the regex to end with a word boundary:\.domain\.org\b(?:[^\s=]|=\s)*
内容的提问来源于stack exchange,提问作者Luis A.

