MacOS hosts file entry for only non-www url
Asked Answered
B

2

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.

Burnsides answered 30/12, 2016 at 6:27 Comment(10)
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.Bullbat
@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.Burnsides
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.Bullbat
Neither of those commands produce any output. Is that expected?Burnsides
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.Bullbat
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.Bullbat
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.Burnsides
The hosts file doesn't have any unexpected characters in it. Thanks for providing that easy way of checking.Burnsides
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".Bullbat
It does: "www.example.com is an alias for example.com."Burnsides
H
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.

Hypocycloid answered 13/3, 2017 at 21:38 Comment(4)
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.Burnsides
The hosts file is queried before any dns queries. And as described, it expands domains to subdomains.Pause
@fragmentedreality: Of course, it is read before that. Yet, dnsmasq's host option supersedes the hosts file, see its man page: host-record options are considered to be read before host-files [emphasis mine]Hypocycloid
I was addressing @Burnsides to point out to him why the subdomain is resolved to the TLD. Nevertheless thank you for the detailed update.Pause
P
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.

Pause answered 13/3, 2017 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.