Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS hosts file entry for only non-www url

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.

like image 526
Jason Avatar asked Dec 30 '16 06:12

Jason


People also ask

How do I change permissions on hosts file Mac?

Edit Hosts File Open the hosts file for editing. In the terminal window you just opened copy/paste the command string below, and press return. sudo nano /private/etc/hosts or sudo nano /etc/hosts on newer MacOS. Enter your password when it prompts you to enter your Mac user's password.


1 Answers

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.

like image 132
serv-inc Avatar answered Sep 28 '22 19:09

serv-inc