Force safe search using Pi-hole

Moderator note:
This post contains scripts, or links to scripts, that are untested by the Pi-hole team.
We encourage users to be creative, but we are unable to support any potential problems caused by running these scripts

To force safe search using Pi-hole just like what AdGuard Home does, run the following commands.

########################################################################################################################################
# Safe Search                                                                                                                          #
# All records copied from https://github.com/AdguardTeam/AdGuardHome/blob/9f0fdc5e78331a9486c1be2de2d062abff63deb7/internal/filtering/safesearch.go for use with Pi-hole #
########################################################################################################################################

# DuckDuckGo
sudo pihole -a addcustomcname duckduckgo.com safe.duckduckgo.com false
sudo pihole -a addcustomcname www.duckduckgo.com safe.duckduckgo.com false
sudo pihole -a addcustomcname start.duckduckgo.com safe.duckduckgo.com false

# Bing
sudo pihole -a addcustomcname www.bing.com strict.bing.com false

# Google
sudo pihole -a addcustomcname www.google.ad forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ae forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.al forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.am forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.as forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.at forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.az forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ba forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.be forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bf forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bi forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bj forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bs forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.bt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.by forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ca forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cd forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cf forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ch forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ci forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ao forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.bw forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ck forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.cr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.id forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.il forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.in forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.jp forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ke forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.kr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ls forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ma forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.mz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.nz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.th forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.tz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ug forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.uk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.uz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.ve forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.co.vi forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.af forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ag forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ai forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ar forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.au forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.bd forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.bh forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.bn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.bo forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.br forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.bz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.co forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.cu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.cy forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.do forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ec forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.eg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.et forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.fj forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.gh forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.gi forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.gt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.hk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.jm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.kh forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.kw forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.lb forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ly forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.mm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.mt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.mx forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.my forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.na forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.nf forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ng forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ni forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.np forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.om forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.pa forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.pe forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.pg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ph forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.pk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.pr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.py forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.qa forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.sa forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.sb forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.sg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.sl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.sv forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.tj forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.tr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.tw forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.ua forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.uy forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.vc forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.com.vn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cv forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.cz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.de forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.dj forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.dk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.dm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.dz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ee forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.es forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.fi forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.fm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.fr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ga forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ge forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gp forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.gy forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.hn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.hr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ht forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.hu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ie forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.im forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.iq forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.is forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.it forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.je forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.jo forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.kg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ki forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.kz forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.la forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.li forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.lk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.lt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.lu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.lv forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.md forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.me forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ml forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ms forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mv forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.mw forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ne forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.nl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.no forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.nr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.nu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.pl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.pn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ps forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.pt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ro forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.rs forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ru forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.rw forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sc forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.se forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sh forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.si forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.so forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.sr forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.st forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.td forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tk forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tl forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tm forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tn forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.to forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.tt forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.vg forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.vu forcesafesearch.google.com false
sudo pihole -a addcustomcname www.google.ws forcesafesearch.google.com false

# YouTube
sudo pihole -a addcustomcname m.youtube.com restrictmoderate.youtube.com false
sudo pihole -a addcustomcname www.youtube-nocookie.com restrictmoderate.youtube.com false
sudo pihole -a addcustomcname www.youtube.com restrictmoderate.youtube.com false
sudo pihole -a addcustomcname youtube.googleapis.com restrictmoderate.youtube.com false
sudo pihole -a addcustomcname youtubei.googleapis.com restrictmoderate.youtube.com false

# Pixabay
sudo pihole -a addcustomcname pixabay.com safesearch.pixabay.com false

# Yandex
sudo pihole -a addcustomdns 213.180.193.56 www.yandex.by false
sudo pihole -a addcustomdns 213.180.193.56 www.yandex.com false
sudo pihole -a addcustomdns 213.180.193.56 www.yandex.kz false
sudo pihole -a addcustomdns 213.180.193.56 www.yandex.ru false
sudo pihole -a addcustomdns 213.180.193.56 www.yandex.ua false
sudo pihole -a addcustomdns 213.180.193.56 yandex.by false
sudo pihole -a addcustomdns 213.180.193.56 yandex.com false
sudo pihole -a addcustomdns 213.180.193.56 yandex.kz false
sudo pihole -a addcustomdns 213.180.193.56 yandex.ru false
sudo pihole -a addcustomdns 213.180.193.56 yandex.ua false

# Important! Restart DNS after all the records are added
sudo pihole -a restartdns

Thank you for your contribution..

I'm afraid that your approach will not work reliably. :frowning_face:

Regarding the custom Local DNS Records for Yandex, you may verify this e.g. by running e.g.:

~$ nslookup yandex.com
Server:         192.168.1.20
Address:        192.168.1.20#53

Name:   yandex.com
Address: 213.180.193.56
Name:   yandex.com
Address: 2a02:6b8:a::a

IPv6-capable clients may by-pass your redirection attempts by simply using Yandex's IPv-6 addresses.

All of your remaining redirection attempts rely on CNAME records.
Local CNAME Records prominently disclaims

The target of a CNAME must be a domain that the Pi-hole already has in its cache or is authoritative for. This is a universal limitation of CNAME records.

Resolution requests for your CNAME redirections would result in Pi-hole returning the CNAME target, e.g.

~$ nslookup pixabay.com
Server:         192.168.1.20
Address:        192.168.1.20#53

pixabay.com     canonical name = safesearch.pixabay.com.
;; Got SERVFAIL reply from 192.168.1.20, trying next server
** server can't find safesearch.pixabay.com: SERVFAIL

Client software expecting an IP address, like browsers, will acknowledge this with an error message.

To make CNAMEs work, you'd have to define the respective local DNS records (A and AAAA) for resolution targets as well.
If you consider this, you should be aware that when acquiring the IP addresses to add, the IPs you would receive for resolving a given domain may differ from those that someone requesting it from another geographical location may see.
Also, this approach may break as soon as public DNS resolution of such a target domain would change to a different IP.

To temporarily mitigate this, you could do manual lookups for the CNAME target domains:

nslookup safe.duckduckgo.com <your.pihole.host.ip>
nslookup strict.bing.com <your.pihole.host.ip>
nslookup forcesafesearch.google.com <your.pihole.host.ip>
nslookup restrictmoderate.youtube.com <your.pihole.host.ip>
nslookup safesearch.pixabay.com <your.pihole.host.ip>

Substitute <your.pihole.host.ip> with your respective IP address.

This will make the CNAME target records known to Pi-hole by injecting them into Pi-hole's cache. Until the TTL of those records expires, your CNAME redirects will then work.
You could probably run a cron job executing those nslookups every 5 minutes or so (currently, 300 seconds seems to be the lowest TTL for above safe search domains).