There are several questions about this, but none seem to address what I'm seeing.

After editing my /etc/hosts with vi with a test redirect, nothing is redirecting when I visit in a browser, including Safari, Chrome and Firefox. This is on MacOs Sierra.

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
## localhost broadcasthost
::1             localhost   www.cnn.com/

the result of $ cat /private/etc/hosts shows the same contents.

I have tried flushing the cache two ways:

$ sudo killall -HUP mDNSResponder
$ dscacheutil -flushcache

I have also rebooted my machine.

How can I get my /etc/hosts file to redirect?


It's extremely picky about the format of the /etc/hosts file. You have a "/" after the domain name, and this will confuse it (actually, it'll think that's part of the host name, and since you're trying to connect to the host "www.cnn.com" not "www.cnn.com/", it just doesn't match). So just remove the "/" and you should be good. There's no need to reset any caches.

| improve this answer | |
  • Welp, that worked. What about sites that don't have www? For example, " imgur.com" as an entry isn't redirecting, even without the trailing forward slash. – steel Apr 4 '17 at 1:21
  • 1
    @steel That looks like it should work. There might be some invisible characters causing trouble, though; try printing it with LC_ALL=C cat -vet /etc/hosts. That'll show tabs as "^I" and newlines as "$"; if you see anything else in there, that's probably the problem. – Gordon Davisson Apr 4 '17 at 1:34
  • The reason imgur.com doesn't work in your hosts file is that it isn't a complete host name, it's a domain name. You must have the entire fully qualified domain name in hosts. When you type imgur.com into your browser the domain tells the browser to redirect to www.imgur.com - the FQDN. – Tony Williams Apr 4 '17 at 8:17
  • @TonyWilliams A host name is just a domain name with a computer ("host") associated with it. imgur.com has an address (A) record in DNS, and there's a computer at that address, so it's a hostname; even if it didn't, adding it to /etc/hosts would make it function as a hostname. Also, www.imgur.com redirects to plain imgur.com. – Gordon Davisson Apr 5 '17 at 1:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .