How do I block ads on YouTube?

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.

@Stephen304

I successfully use "\ S" (any non-whitespace) in my regex.
Of course you can also write [a-zA-Z0-9].
I also came to the conclusion that with Pi-hole it is almost impossible to successfully block advertising in Youtube. Youtube is doing a very good job.

Speedy70

Hmmm I'm not sure why your form of the regex doesn't work in that case. I've decided to give up on using pihole to block youtube ads and went with youtube vanced instead - that seems to be working great. I'll just keep pihole to catch everything else, but I'm now mostly reliant on uBlock origin and other browser addons.

@Stephen304
Unfortunately, I'm not a regex specialist either.
But originally I also had problems with the spelling. That's why I decided to build the regex so that it does not match the beginning of the domain "r0 ---".

just tested in Pi-hole ... With this spelling match the regex the domains ...

grafik

grafik

yt

@kliopa10

Hello
Since your information has turned out a bit bulky, I suspect you want to use your regex to filter domains that contain the text -q0c?.googlevideo.com ?

Oh well.
I would use the following regex for this:

grafik

or

grafik

Speedy7090

I've found that the only way to get rid of youtube ads is to buy their premium subscription.
I've bought mine through a VPN in a european country so am only paying $4.5 pr. month.

Not sure if this will do anything for me or not. I have no regex experience.

r5.sn-vgqsknez.googlevideo.com
r2.sn-vgqsrnll.googlevideo.com
r2.sn-vgqskned.googlevideo.com
r1.sn-vgqsenls.googlevideo.com
r2.sn-vgqs7ns7.googlevideo.com

i suppose this part?

sn-vgqs

they all have an n too huh..

nez.googlevideo.com
nll.googlevideo.com
ned.googlevideo.com
nls.googlevideo.com
ns7.googlevideo.com

I am pretty sure pi-hole lies. I wrote this regex: * r[0-9]+-+sn-[\w\d]{8}.googlevideo.com which according to regex101.com is an exact match to the URL's YouTube goes to and it still doesn't work.

Pi-Hole uses POSIX ERE regex. Other forms of regex will pass the checks at regex101 and will not work with Pi-Hole. To determine conclusively if your regex will work with Pi-Hole, enable regex debugging per the Pi-Hole documentation, and then dig a domain that you think should be blocked with your new regex. If the domain is blocked by the regex, there will be an entry in /var/log/pihole-FTL.log.

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

1 Like

for information

Youtube seems to use new video domains.

r3---sn-uxax3vh50nugp5-8px6.gvt1.com

Speedy70

1 Like

So can someone please explain, how does ublock/adblock successfully block ALL youtube ads (at least in Waterfox/Firefox)? Why can't pi-hole do the same?

[EDIT]
nvm pi-hole is not aware of full url...

r[0-9]+.sn-vgqs[\w\d]+\.googlevideo\.com

1 Like

:hugs: thanks buddy!