I have blocked some websites by editing /etc/hosts and adding several newline-entries in the following manner:
0.0.0.0 www.domain.com
And it works. The only thing is: when a website is visited which is blocked, the browser is redirected to my http://localhost
, resulting in a directory listing or website-presentation that is running within my localhost root-environment.
It's not a very big problem, but I prefer a standard error that mentions the website cannot be visited (for instance by a 404 page). Is this possible?
I assume you're running a local web server? It seems like you are, so I will assume that. Also, I'm hoping you're using Apache, although other web servers probably have the same capabilities, but I don't have experience with any others.
If you are blocking a large number of sites, this might become too much trouble, but you could probably create a virtual domain with a start page for your 404 page. If you know PHP, you could probably customize it with the name of the site that's been blocked for a more informative message.
The hosts file would stay the same, and the additions would be made to Apache's configuration file in
/etc/apache2/sites-enabled
. Initially, there is only the document root, defined, I think, as/var/www
; you can not only change this, but you can add any number of additional directories with associated names and aliases.My idea is to add one for a new directory that would contain your 404 page. Decide on a directory and a "site name", and add a new entry to the file. For example:
You can add an entry to your hosts file with
localhost my404.loc
, and then you will be able to access the page locally for testing by enteringhttp://my404.loc
. Hopefully, you can also access it bywww.domain.com
if you have that entry in your hosts file. You should be able to keep adding more "ServerAlias" entries as you add more to the hosts file.I hope this is clear to you, and maybe even helpful. If you think it's helpful, but it isn't clear, feel free to ask more.
No guarantees, as I've never actually tried this whole thing. :)