How do I block ads on YouTube?

@duceduc

Hello

try this regex. :slightly_smiling_face:
grafik

greetings
Speedy

@Speedy70
Thank you mate, your regex helped a lot.

r1---sn-ux3n588t-mjhl.googlevideo.com
r4---sn-ux3n588t-mjhe.googlevideo.com
r3---sn-ux3n588t-mjhz.googlevideo.com
r4---sn-ux3n588t-mjh6.googlevideo.com
r3---sn-ux3n588t-mjh6.googlevideo.com


.*sn-\S{4,}-\S{3}[0-9]\.googlevideo\.com

For me, only the last two was serving ads and the rest serving videos.
So I had to modify the regex slightly so that it matches "mjh"+ number.

1 Like

Cheers @kajal09 and @Speedy70, going to try this regex as well.

Gave this a test today and they seem to be serving the ads from the same domain as the video. Any progress on your testing?

Anyone assist to make a regex for this:

r6---sn-h5mpn-30ad.googlevideo.com
r1---sn-h5mpn-30al.googlevideo.com
r6---sn-h5mpn-30ad.googlevideo.com
r1---sn-h5mpn-30al.googlevideo.com
r3---sn-h5mpn-30ay.googlevideo.com
r6---sn-h5mpn-30ad.googlevideo.com

Thank you.

also for this two

r4---sn-4g5ednsz.googlevideo.com
r1---sn-hgn7yn76.googlevideo.com

@ssneoh

Try this regex
grafik

This should match the following variants of your domains.
grafik

Speedy70

@afikzach

Since I can not recognize a clear search pattern for your two domains, I advise you to simply put these domains in the blacklist (Pi-hole ---> Frontend ---> Blacklist ---> Add exact).
Building a matching regex does not really make sense, since almost all video links on Youtube have this structure.
Thus, a regex would match your two video links, synonymous match all other videos with the same link structure and thus block.

Speedy70

correct me if i'm wrong:

r4—sn-4g5ednsz.googlevideo.com
r1—sn-hgn7yn76.googlevideo.com

r[0-9-]*-sn-[a-z0-9]{8}.googlevideo.com

is it correct syntax ? just made it up according to my logic nut i'm not sure

@afikzach

Hello
Your regex would not work because [0-9-] is not a valid expression in my opinion.
Try this regex.
grafik

This should match the following variants of your domains.
grafik

Look at this website https://regex101.com
Here you can try very well, if your regex works and which inputs are matched.

Speedy70

thank you i'll check it

Thank you.

I wasn't able to get your regex to work, I suspect it might be something with the pihole regex engine and the regex features you used. It works fine in regex101's tester, but pasting the exact same in pihole with regex debugging on results in no logged matches and those queries getting through. I reworked it more crudely as r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com, but still no success. Checking my logs it's catching every rx-sn-xxx subdomain and doesn't seem to be blocking anything extraneous according to the regex debug log, but now videos won't load at all. I wonder if youtube has started serving the videos themselves using these random subdomains? At least in the android app, I don't use pihole for laptops/desktops that can use uBlock origin.

Edit: Or, another thing they could have done is refuse to load the video until the ad successfully loads. That would be pretty shitty.

Pi-Hole uses POSIX ERE regex. Everything that passes Regex 101 does not necessarily work in Pi-Hole. The best approach is to enable regex debug mode, then your /var/log/pihole-FTL.log will contain details on which regex is triggered by which request.

DEBUG_REGEX=true

https://docs.pi-hole.net/ftldns/configfile/

@Stephen304

I use the following syntax in PI-hole to block add-ons in Youtube.
grafik
But even this regex is not a 100% guarantee that no more add-ons will be loaded. With Pi-Hole, an ad-free Youtube is not 100 percent possible. Only browser addons help!

Greeting Speedy70

Thanks for confirming my suspicions. In case you didn't catch from my previous message, I already have debug mode enabled, that's how I was determining what was being blocked or not.

Getting the regex to work is not the problem, if you see my previous message, I got it to work just by recreating the regex using different patterns. Using your regex doesn't work for me seemingly due to what @jfb said, but that's a moot point.The problem is that blocking all the rx--sn-xxxxx subdomains also blocks the video from loading.

For example, here is the pihole-FTL log when attempting to load a video with an ad:

[2019-08-18 14:42:27.519 636] Regex in line 2 "r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com" matches "r2---sn-nx57ynls.googlevideo.com"
[2019-08-18 14:42:28.387 636] Regex in line 2 "r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com" matches "r4---sn-nx57ynls.googlevideo.com"
[2019-08-18 14:42:28.423 636] Regex in line 2 "r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com" matches "r4---sn-vgqsrn7s.googlevideo.com"
[2019-08-18 14:42:35.014 636] Regex in line 2 "r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com" matches "r5---sn-nx57ynl6.googlevideo.com"
[2019-08-18 14:42:35.020 636] Regex in line 2 "r[0-9]+[-]+sn[-]+[A-Za-z0-9]+\.googlevideo\.com" matches "r5---sn-nx5e6nez.googlevideo.com"

And here is my query log showing that only the domains with this pattern are blocked, nothing else is getting caught: (i relaunched the app several times which is why this one goes before 14:42:27, but the result is the same every time)

2019-08-18 14:43:15 	A	r2---sn-nx5e6ne6.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.5ms)	
2019-08-18 14:43:03 	A	r2---sn-vgqsrn7d.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.5ms)	
2019-08-18 14:42:39 	A	checkip.amazonaws.com	192.168.1.239	OK (forwarded)	CNAME (15.8ms)	
2019-08-18 14:42:35 	A	r5---sn-nx57ynl6.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.5ms)	
2019-08-18 14:42:35 	A	r5---sn-nx5e6nez.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.3ms)	
2019-08-18 14:42:28 	A	r4---sn-nx57ynls.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.5ms)	
2019-08-18 14:42:28 	A	r4---sn-vgqsrn7s.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.4ms)	
2019-08-18 14:42:27 	A	r2---sn-nx57ynls.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.5ms)	
2019-08-18 14:42:22 	A	redirector.googlevideo.com	192.168.1.239	OK (cached)	IP (0.1ms)	
2019-08-18 14:42:22 	A	r2---sn-nx5s7n7s.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.1ms)	
2019-08-18 14:42:22 	A	r5---sn-nx5s7n76.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.1ms)	
2019-08-18 14:42:11 	A	redirector.googlevideo.com	192.168.1.239	OK (cached)	IP	
2019-08-18 14:42:11 	A	r2---sn-nx5s7n7s.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.4ms)	
2019-08-18 14:42:11 	A	r6---sn-nx5e6n76.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.2ms)	
2019-08-18 14:42:11 	A	r4---sn-nx5s7n76.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.2ms)	
2019-08-18 14:42:04 	A	redirector.googlevideo.com	192.168.1.239	OK (forwarded)	IP (0.4ms)	
2019-08-18 14:42:04 	A	r3---sn-nx5e6nes.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.6ms)	
2019-08-18 14:42:04 	A	r5---sn-nx57ynls.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.3ms)	
2019-08-18 14:42:04 	A	r3---sn-nx5s7n7z.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.3ms)	
2019-08-18 14:42:04 	A	r5---sn-nx5s7n76.googlevideo.com	192.168.1.239	Blocked (regex/wildcard)	- (0.4ms)

And here is what happens on my phone:

...look at https://discourse.pi-hole.net/t/how-do-i-block-ads-on-youtube/253/390?u=speedy70

For this reason I do not use a regex of domains with this format match. This of course means that sooner or later no more videos are displayed.
I have found special domains which have a different structure and in my opinion are also responsible for advertising.

The format looks like this ...
grafik

Currently no videos will load, and adding exact match for a subset makes no impact - I get an ad on every video that has an ad. But blocking all of them results in no videos loading, even videos that don't have ads.

I'm checking the query log for anything that looks different but I don't see anything, I relaunched the youtube app multiple times and force closed it in between and saw plenty of ads, so it should be here somewhere:

2019-08-18 14:54:58 	A	r4---sn-nx57ynls.googlevideo.com	192.168.1.239	OK (cached)	CNAME	
2019-08-18 14:54:57 	A	r3---sn-nx5s7n7s.googlevideo.com	192.168.1.239	OK (cached)	CNAME (0.1ms)	
2019-08-18 14:54:56 	A	redirector.googlevideo.com	192.168.1.239	OK (cached)	IP (0.1ms)	
2019-08-18 14:54:56 	A	r2---sn-nx5e6nes.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (41.5ms)	
2019-08-18 14:54:56 	A	r5---sn-nx5s7n7s.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (41.0ms)	
2019-08-18 14:54:56 	A	r3---sn-nx5e6n76.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (45.6ms)	
2019-08-18 14:54:45 	A	r4---sn-nx57ynls.googlevideo.com	192.168.1.239	OK (cached)	CNAME	
2019-08-18 14:54:44 	A	r4---sn-nx5e6nez.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (66.9ms)	
2019-08-18 14:54:39 	A	redirector.googlevideo.com	192.168.1.239	OK (cached)	IP (0.1ms)	
2019-08-18 14:54:39 	A	r5---sn-nx5s7n7z.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (67.4ms)	
2019-08-18 14:54:39 	A	r1---sn-nx5s7n7s.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (46.2ms)	
2019-08-18 14:54:39 	A	r5---sn-nx5e6ne6.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (40.3ms)	
2019-08-18 14:54:16 	A	r3---sn-nx57ynlz.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (71.7ms)	
2019-08-18 14:54:12 	A	r6---sn-nx5e6nes.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (62.1ms)	
2019-08-18 14:54:08 	A	r4---sn-nx5e6n76.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (70.0ms)	
2019-08-18 14:54:08 	A	r4---sn-nx5s7n76.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (65.8ms)	
2019-08-18 14:54:05 	A	r4---sn-nx57ynls.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (61.8ms)	
2019-08-18 14:54:04 	A	r2---sn-nx57ynls.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (0.4ms)	
2019-08-18 14:54:02 	A	r3---sn-nx5e6ne6.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (62.0ms)	
2019-08-18 14:54:02 	A	r4---sn-nx5e6ne6.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (65.3ms)	
2019-08-18 14:54:02 	A	r3---sn-nx5s7n7s.googlevideo.com	192.168.1.239	OK (forwarded)	CNAME (66.7ms)	
2019-08-18 14:54:01 	A	redirector.googlevideo.com	192.168.1.239	OK (forwarded)	IP (21.2ms)	

@Stephen304
Unfortunately, I do not use the Youtube app. But it can not be ruled out that the app uses domains other than a browser for the Viedeo ad. In the browser I get a lot of videos with these special domains. Therefore I blocked it too. And I have the feeling that I get - noticeably - less advertising. But here too. The same advertisement is not displayed in every country. I am e.g. at home in Austria. Or. What works for one user does not necessarily work for all the others.

For me, this regex works just fine.

I'm not sure if \S is posix ERE, since your pattern doesn't match anything for me even though the domains I load should match. See jfb's comment above. For me, uBlock Origin is all I need to get rid of 100% of youtube ads in browser, so pihole is just for phones for me. Which is unfortunate that it seems to not be making a difference for me.