Exclude subdirectory from host resolution set in /etc/hosts (with dnsmasq, or anything else)

I have a local development tool (LocalWP) that sets the path to my local environment by modifying /etc/hosts.

For example, if I set my site in Local to www.foo.com, it'll append the following to the hosts file:

127.0.0.1 www.foo.com

This is fine, but I need to exclude a subdirectory (not a subdomain) in this domain, so it resolves to another (the original) IP.

Of course, this isn't possible with etc/hosts because it is just a DNS pointer and doesn't resolve anything except the domain. However, I'm wondering if there's another tool I can use to exclude a subdirectory from the current hosts file, and point it to a new IP.

I'm wondering if something like dnsmasq can accomplish this, or if there's another tool I could use for some conditional DNS behavior based on the resolution of the path, rather than just the domain.


Solution 1:

You can't do that.

Name resolution is handled by DNS (or /etc/hosts, or dnsmasq, or whatever else), but everything after the first / is part of the HTTP protocol, and as such is handled by your web browser (on your side) and the web server (on the other side); DNS (or /etc/hosts, or dnsmasq, or whatever else) is only used to find the IP address associated with the host name, which will always be the same regardless of the subdirectory you are trying to access; the actual HTTP path will only be used after the connection is established.