0

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 127.0.0.1. 127.0.0.1/blocked/ 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, example.com for instance, to http://localhost/blocked/.

2 Answers 2

1

Well, here's something you could consider:

  • Make entries in your hosts file for all the blocked domains, pointing to 127.0.0.1 (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
    ServerAlias example.com
    DocumentRoot /var/www/blocked.localhost/htdocs
    # put the usual <Directory> section for that document root
</VirtualHost>

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)

1

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.

2
  • 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, 2010 at 4:25
  • 1
    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, 2010 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .