Why is Safari ignoring my /etc/hosts file?

I had a slightly different version of the same problem, and thought I'd mention what worked for me.

I develop websites. To do that, I have a complete copy of each site running on my local network. When I wish to work locally, I have always just added an entry for "example.com" to my local /etc/hosts file, and that has always overridden DNS, making it possible for me to access the local copy of the website in any browser, including Safari and Firefox. I can work without affecting the actual live site, then upload changes when they're complete.

Recently, however, this technique stopped working for both Safari and Firefox, but not for Chrome, so for a while I was limited to using Chrome for local development. I searched google and tried all the suggested fixes -- CR at the end of the hosts file, only one entry per line, various Firefox configuration changes using "about:config", etc. Nothing worked.

Then I tried this simple thing: in addition to putting the IPV4 version of the address in the /etc/hosts file, I also put the IPV6 version.

Before:

10.0.1.23       example.com

After:

10.0.1.23       example.com
0:0:0:0:0:FFFF:0A00:0117        example.com

As soon as I added the IPV6 entry, both Firefox and Safari started hitting the correct (local) server.

There are various websites that will translate IPV4 addresses to IPV6; just do a google search on "ipv4 to ipv6".


I spent quite some time on OS X El Captain 10.11.4 to block sites using /etc/hosts file instead of using Parental Control in System preferences. At the end it simply worked like this:

127.0.0.1 www.website.com
127.0.0.1 website.com

I tried a lot of variants, but I was always mapping to 0.0.0.0 (0.0.0.0 website.com). That never worked in any variation.

Unlike some suggestions on the web, these were useless:

  • There was no need for IPv6 blocking like fe80::1%lo0 www.website.com or ::1 website.com
  • There was no need for dscacheutil -flushcache, but just do it in case.
  • It does not matter if you paste it at the beginning or end of the hosts file
  • Browsers seem to have been responded differently: Chrome and Safari blocked it immediately once the correct hosts file was saved, Firefox kept ignoring it for a while (not clear if restarting it was enough).
    • Try to use New Private Window or restart the browser for testing it.
  • Do not add http:// or https:// at the beginning of the address
  • No other commands were necessary.
  • Do not disable (= comment out by adding a # at the beginning) lines that are in the hosts file already.

Useful checks

  • Check if the system recognizes your redirection dscacheutil -q host -a name website.com - it should display:

    name: website.com
    ip_address: 127.0.0.1
    
  • Check if the syntax of the hosts file is correct by: cat -vet /etc/hosts. This shows invisible characters:

    • Lines should end with $
    • Between 127.0.0.1 and website.com should be only a space or a tab, which is displayed as ^I.
  • Note that for some sites, like facebook for instance, you might need to block a lot of addresses.
  • I think that the hosts file should end with a newline.