Redirect with regex

regex

#1

I’m trying to redirect YouTube ad servers. I have the following regex thanks to pk198105:

(^r[[:digit:]]+(\.|\-+)[[:alnum:]]+\-+[[:alnum:]]+\.)(googlevideo|gvt1)\.com$

The problem is that this blocks the ad domains and the YouTube client on my smart TV goes into a loop looking for working servers. However, someone else found that if you simply redirect these requests to googlevideo.com they are handled and the video plays.

So I need to redirect everything matching the regex to googlevideo.com.


#2

You won’t be able to do that with Pi-Hole without modifying your local code.

Pi-Hole either blocks or doesn’t block a domain. There is no mechanism for redirect.

If you were to set up a proxy and have the proxy examine the domain, you could filter on this regex.


#3

What about BLOCKINGMODE? That lets you select redirecting blocked domains to the Pi’s local IP address and a page where you can whitelist. Is there some way I can change that to redirect everything to do googlevideo.com instead?


#4

I don’t think you can redirect, using regex, however, A solution for individual redirects can be achieved. This has been explained here, for safe search.

for your redirect, this would imply this hosts (/etc/hosts) entry:

172.217.17.36	googlevideo.com 

or whatever googlevideo.com resolves to in your area

and a dnsmasq configuration file (/etc/dnsmasq.d/97-redirect.conf), containing the following:

cname=<the domain name you want to redirect>,googlevideo.com

you can enter as many redirections as you want to, just duplicate the cname entry and change <the domain name you want to redirect>

The down side of this approach is you will have to restart pihole-FTL every time you change the configuration file.

Don’t know if this a solution for what you’re trying to achieve…


#5

Thanks jpgpi250. I was aware you can do it that way with IP or domain redirects, but the problem is that Google has a vast number of servers offering up Google video and there is no way to find them all, hence the regex.

Someone made a script that adds the advertising domains after the first time they are used, but obviously it’s not as robust as the regex.

I’d be happy to have a PiHole just for YouTube where all blocked domains are redirected to googlevideo.com. Maybe there is some way to reconfigure what BLOCKINGMODE=IP does.


#6

If you changed the IP that is returned for blocked domains, then all blocked queries would go to that IP. It is better to do as @jpgpi250 suggests.


#7

That would be fine Mcat12. Is there some way I can do that?

I’ll have a PiHole just for YouTube.


#8

No, that is not a feature.


#9

Okay, I’ll open a feature request. If I have time I’ll look at contributing code.


#10

NOT sure this will work. When using blocking mode IP, the IP addresses (IPv4 and IPv6) are read from /etc/pihole/setupVars.conf. You could try changing the IP addresses in this file, EVERY bllocked domain would than be redirected to these IPs

NOT sure what happens if the weekly pihole -g OR pihole -up runs. Pretty sure pihole -r will undo the configuration change.


#11

Hmmm… Well I’m sure we can fix the configuration overwrite. Isn’t the IP addressed in setupVars.conf used for the web UI though? I’ll give it a try anyway.