Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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:

<VirtualHost *:80>
    <Directory /var/www/my404>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    DocumentRoot /var/www/my404/
    ServerName my404.loc
    ServerAlias www.domain.com
</VirtualHost>

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 entering http://my404.loc. Hopefully, you can also access it by www.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. :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.