Exclude phrase ending with matched pattern using regex

Viewed 57

I have written a rule to catch all web domains ending with .watch or .video, and I want to exclude 2 domains:

(?!\.videolecture|fb\.watch)\b(\.video|\.watch)

The first exclusion .videolecture works fine. But I can't exclude fb.watch.

I'm really sorry but I could't find any similar questions on stackoverflow..

2 Answers

You can use

\b(\.video(?!lecture\b)|(?<!\bfb)\.watch)

See the regex demo. Details:

  • \b - a word boundary
  • ( - start of a capturing group:
    • \.video(?!lecture\b) - .video that is not immediately followed by lecture as a whole word
    • | - or
    • (?<!\bfb)\.watch - .watch that is not immediately preceded with fb as a whole word
  • ) - end of the group.

An exclude variable could be set using a map. To exclude second-level domains, videolecture or fb, and top-level domains, watch or video:

map $host $exclude {
    ~\b(?:videolecture|fb)\.(?:watch|video)$ 1;
}

Return 403 if $exclude is set:

server {
    if ($exclude) {
        return 403
    }
}
Related