Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory setup with a nice error message at http://localhost/blocked/. I'd like to direct entries in my hosts relating to sites I've blocked to that instead of just doesn't work. I'm clueless about the host file and this may not even be the best way to accomplish my goal.

To restate: I'd like to direct all requests on certain domains, for instance, to http://localhost/blocked/.

share|improve this question
up vote 1 down vote accepted

Well, here's something you could consider:

  • Make entries in your hosts file for all the blocked domains, pointing to (I suppose you're already doing this)
  • Make a <VirtualHost> block in your Apache configuration that has a ServerAlias line for each of the blocked domains. Or one of them could be a ServerName and the rest ServerAliases. In this virtual host, you can either redirect all requests to the existing URL of your "blocked" error page, or (perhaps simpler) just put the error page in the document root.

Here's how I'd do it:

<VirtualHost *:80>
    ServerName blocked.localhost
    DocumentRoot /var/www/blocked.localhost/htdocs
    # put the usual <Directory> section for that document root

and move the whole contents of the blocked/ directory (on your filesystem) to /var/www/blocked.localhost/htdocs. Obviously the pathnames are just examples.

Make sure a NameVirtualHost *:80 line appears somewhere in your Apache configuration. (Or if you want to catch requests on ports other than 80, that can be arranged as well)

share|improve this answer

The hosts file is only concerned about well, hosts. Not directories within that host. In order to do what you're hoping, you'll need to do a redirect from http://localhost/ to http://localhost/blocked/. You can accomplish this with mod_alias or mod_rewrite if you're running apache.

share|improve this answer
Yes, but I don't want all visits to localhost to redirect to the blocked page. I'm more familiar with mod_alias and mod_rewrite but I can't see how those would be useful. As I understand, moving the contents of blocked into the main Apache directory for localhost would have the same effect. Thanks though. – Regis Frey Jun 28 '10 at 4:25
Well then you're going to need to get some other technology into the mix, as the hosts file plain and simple won't do what you're hoping it will. Perhaps consider getting squid set up, with which you could easily block and redirect certain sites. – EEAA Jun 28 '10 at 4:29

Your Answer


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.