I'm working locally on a project and want to have some requests to a domain resolve to localhost and others just go where they normally would.

I want to essentially do this in my hosts file: www.example.com/dir/* www.example.com

As far as I understand, this isn't possible with /etc/hosts, but I could be wrong. Nevertheless, I'm open to suggestions. I'm using Apache and have tried using a proxy to send /dir/* requests to the real IP address of www.example.com, but then they can't resolve where I came from—I need it to seem like I'm accessing www.example.com by going directly to www.example.com.

I.e., this doesn't do what I want in my site configuration:

ProxyPass /dir/

Because then the foreign site thinks I'm accessing it from and not example.com.

2 Answers 2


As far as I understand, this isn't possible with /etc/hosts, but I could be wrong

You understood that correctly.

DNS (and /etc/hosts is part of the DNS resolver) resolves hosts.
It does not do anything with URLs.

In www.example.com/dir/* is not the name of a computer/host, so it should throw an error or ignore the line.

This is normal DNS operation and this is just how it works.

Confusingly HTTP is an odd duck. the HTTP protocol does look at server names and directories. The host will use DNS to resolve the IP, and then sent a datagram which includes the URL. This is why you can have different rules for different directories when you play with proxies (or even multiple website domains on a single IP).

But as written before. HTTP is the odd duck here. And first it will need to resolve the IP via DNS.

I pondered posting this as a comment since it does not really help you with the problem, other then understanding where things go wrong. Sadly comments do not allow a lot of formatting. :(

  • Good information, and solidifies a few of my assumptions. Thank you. Is it possible to set up something like a DNS interceptor to catch certain domains?
    – Ian Hunter
    Apr 6, 2013 at 15:48
  • You might be able to do something with a proxy like squid. That way you have something which understands HTTP and you may be able to redirect some of the domains. However that is outside my experience and does not involve the host file. You might want to check if there are similar posts and if there are none create a new post.
    – Hennes
    Apr 6, 2013 at 16:01

I have solved similar problem with setting up a third level domain local.example.com for the test project directory, together with mod_rewrite on Apache and .htaccess rules. The hosts file is: www.example.com local.example.com

and the .htaccess file on corresponding Apache virtual server is RewriteEngine on RewriteCond %{HTTP_HOST} ^local.example.com RewriteRule ^(.*)$ /testdir/$1 [L,NC,QSA]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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