MacOS file entry for non-www only URL

I am running MacOSX 10.11.4.

I want example.com to point to my local Apache server, but www.example.com points to the actual website.

Example: I have the following entry in the / 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 one and same).

It is interesting to note that Chrome will pull the URL the way I want, but Firefox will hit the local host for both.

-

Edit: I know that using something like example.local is more conditional and completely fixes this problem; however, my work has long been using the www / non-www method and, if possible, would like to keep it.

+6
source share
2 answers

It may be too easy, but why don’t you put

 example.com 127.0.0.1 www.example.com 123.52.232.12 

to your /etc/hosts file, and 123.52.232.12 is the IP address of the real site example.com?

why is this happening or how to stop both domains from switching to my hosts file

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

+3
source

When starting OSX, you can add a resolver for this.

Create the directory /etc/resolver/ and add the file for the domain you want to resolve (for example, example.com). Inside the file, you can specify alternative name servers for use in the specified domain. By default, the file name defines the domain (see the Man page 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 converter is applied using the scutil --dns .

Now you need to start a local name server, for example dnsmasq, to ​​resolve queries at 127.0.0.1 . You can 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 about the homebrew solution here .

+2
source

Source: https://habr.com/ru/post/1013626/


All Articles