6

I'm running MacOSX 10.11.4.

I'd like to have example.com point to my locally running apache server, but have www.example.com point to the actual website.

Example: I have the following entry in my /etc/hosts file: 127.0.0.1 example.com If I ping example.com and www.example.com, they both hit 127.0.0.1 (I believe because the canonical URL is recognized as being the same).

Interesting note, Chrome will pull the URL's as I want, but Firefox will hit localhost for both.

-

Edit: I know that using something like example.local is more conventional and avoids this problem entirely; however, my work has been using the www/non-www method for a while now and would like to keep it, if possible.

Jason
  • 546
  • 1
  • 5
  • 21
  • 1
    I think this has to be a Firefox thing, not something to do with the hosts file -- the hosts file doesn't distinguish http vs https, or Firefox vs Chrome, so if it were the problem it'd affect all of the above. – Gordon Davisson Dec 30 '16 at 17:34
  • @GordonDavisson, that was my initial thought; but using `ping` in the terminal results in both going to 127.0.0.1. I would think the two problems (Firefox and the ping command) are related, although it may have nothing to do with the hosts file. – Jason Dec 30 '16 at 23:18
  • That's really weird. What do `dscacheutil -q host -a name example.com` and `dscacheutil -q host -a name www.example.com` report? That goes through the system resolver, so it should tell you what the OS level thinks is going on. – Gordon Davisson Dec 31 '16 at 00:58
  • Neither of those commands produce any output. Is that expected? – Jason Jan 01 '17 at 20:45
  • no, they both should list IP addresses; "example.com" should list 127.0.0.1 (from the hosts file), and "www.example.com" should list the regular DNS-based result. To make sure things are working at all, try `dscacheutil -q host -a name www.apple.com` (it should list a bunch of aliases, and generally both ip_address and ipv6_address. Then try `dscacheutil -q host -a name apple.com`, which should just list several ip_address entries. – Gordon Davisson Jan 01 '17 at 21:21
  • Also, check for invisible characters in /etc/hosts by printing it with `LC_ALL=C cat -vet /etc/hosts`. The entry should look like either "`127.0.0.1^Iexample.com$`" (if you used a tab to separate the IP from the name) or "`127.0.0.1 example.com$`" (if you used a space). If it shows something else, you have something weird in the file and should fix that. – Gordon Davisson Jan 01 '17 at 21:24
  • Okay, here is what it returns for www.example.com: `name: example.com alias: www.example.com ip_address: 127.0.0.1` example.com is the same, except without the alias line. – Jason Jan 02 '17 at 17:37
  • The hosts file doesn't have any unexpected characters in it. Thanks for providing that easy way of checking. – Jason Jan 02 '17 at 17:39
  • 1
    Hmm, in DNS, is www.example.com an alias of example.com? Try `host www.example.com` (which does direct DNS lookups, bypassing /etc/hosts) and see if it says "www.example.com is an alias for example.com". – Gordon Davisson Jan 02 '17 at 19:01
  • It does: "www.example.com is an alias for example.com." – Jason Jan 05 '17 at 01:48

2 Answers2

3

This might be too easy, but why don't you put

example.com     127.0.0.1
www.example.com 123.52.232.12

into your /etc/hosts file, with 123.52.232.12 being the IP address of the real example.com site?

why it's happening or how to stop both domains from going to my hosts file

This is the default resolution order, with the hosts file autoexpanding for subdomains. The solution by @fragmentedreality provides a workaround by using the dnsmasq resolver, which is also recommended at https://serverfault.com/questions/431605/dont-automatically-include-all-subdomains-in-dnsmasq-address and described at https://gist.github.com/ogrrd/5831371.

Community
  • 1
  • 1
serv-inc
  • 29,557
  • 9
  • 128
  • 146
  • 1
    That was my temporary fix, but it doesn't really answer the question of why it's happening or how to stop both domains from going to my hosts file. – Jason Mar 14 '17 at 05:00
  • The hosts file is queried **before** any dns queries. And as described, it expands domains to subdomains. – fragmentedreality Mar 14 '17 at 06:25
  • @fragmentedreality: Of course, it is read before that. Yet, dnsmasq's host option supersedes the hosts file, see [its man page](http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html): *host-record options are considered to be read **before** host-files* [emphasis mine] – serv-inc Mar 14 '17 at 16:03
  • 1
    I was addressing @Jason to point out to him why the subdomain is resolved to the TLD. Nevertheless thank you for the detailed update. – fragmentedreality Mar 14 '17 at 16:13
2

As you are running OSX you can add a custom resolver for that.

Create the directory /etc/resolver/ and add a file for the domain, you want to resolve (i.e. example.com). Inside the file you can specifiy alternative nameservers to use for the specified domain. By default the filename defines the domain (see manpage for more details).

$ sudo mkdir -p /etc/resolver
$ echo "nameserver 127.0.0.1" | sudo tee /etc/resolver/example.com > /dev/null

You can check, if the resolver is applied with the command scutil --dns.

Now you just need to run a local nameserver like dnsmasq to resolve queries to 127.0.0.1. You can either install dnsmasq via homebrew or run it in a docker-container:

$ docker run -p 53:53/tcp -p 53:53/udp --cap-add=NET_ADMIN \
    andyshinn/dnsmasq:2.75 \
    --host-record=example.com,127.0.0.1

Find more information on the homebrew-solution here.

fragmentedreality
  • 1,182
  • 9
  • 27