Cannot Access Localhost After Editing etc/hosts and Flushing DNS Cache

Posted in Apple/Mac , Web Development on 22 November 2011 1 comment

I was developing a new website that uses Facebook Login as the membership system. I came across this situation where I needed to create a “fake” domain by editing the etc/hosts file. Somehow, after editing the etc/hosts file (following an online guide), I cannot seems to browse http://localhost in my browser anymore. However, I can still browse my local apache server using http://127.0.0.1

NOTE:
This is not XAMPP / apache issue
This is not browser-specific issue

In fact, this is a simple problem with simple solution. However, this problem occurs when I am getting frustrated with the Facebook integration problem (after 12 hours of debugging). Therefore, I missed some simple but important steps to solve this problem. Here I list them down below:

1. Ping Localhost

For Mac OS users, open up your Terminal. For windows users, open up your command prompt.

  • Type the “ping localhost” and press enter.
  • You should get error messages like “unknown host” since you are having this problem.
  • Next, type “ping 127.0.0.1″ and press enter.
  • If you saw something like this, the ping is successful:
    PING 127.0.0.1 (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.118 ms
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.171 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.167 ms
  • If you get “unknown host error”, please check whether you have started your local server service.

2. Clear your browser cache

Modern browsers cache a lot of stuff, including DNS cache. They may have cached the erroneous DNS setting in etc/hosts, so you might want to clear your browsers cache and try loading localhost again. Clearing cache alone would already be sufficient, you can skip clearing cookies, saved session, etc.

If it still doesn’t work, try #3 below.

3. Did you just flushed the DNS cache?

I ran this command in terminal (Mac) to flush my cache before I get into this mess: “dscacheutil -flushcache”
For some reason, after I flushed the cache, the DNS setting is not being rebuilt – I cannot ping localhost but I can ping 127.0.0.1

After restarting my laptop (to rebuild the DNS settings & cache) and clearing all browser cache once again, I finally get my localhost displayed again.

Now I hope this helps some of you.

 

Posted by Zen on 22 November 2011 • 53,041 visits 1 comment
Tags :


or Subscribe to specific category only :




  - 1 Comments


Christa Herzog says:

I found this article exciting. The reason is that I try to understand developers work, which is getting more and more difficult. Well, I would not start a website where members login through facebook as there is the possibility that facebook stops it automatically and your members can’t enter anymore. Also I have lost my interest in facebook.

Leave a Reply

You must be logged in to post a comment.

Previous Post
«
Next Post
»