Thursday, March 4th, 2004
Editing Your Windwos Hosts File
You may be wondering what this file is and why you’d ever need to hand edit it. First, what is it. think of it as your own private DNS. Recall that computers haven’t a clue about those handy names we type in our web browsers, for example, pizzabytheslice.com. They want the IP address. DNS is how this is done, which is like a phone book, associating a human-friendly name with a number. (if you’re lost read my web site basics 101 primer, then come back here).
Your computer’s Hosts file is a local DNS file, one that your computer checks before heading out to the Internet to do a DNS lookup. Because it checks this file first this means you can override any web site’s IP address (handy if, say, you want to block all of those annoying ads from doubleclick and other crap sites). It also means you can have bogus, made-up private domains that only work on your computer — and this is perfect for doing web development.
For each of your development sites create a something.localhost entry in your hosts file (localhost is the built-in name for your computer, it’s exactly the same as 127.0.0.1).
Assuming you’re running Apache on your Windows XP/2000/NT machine and you’re developing multiple websites this is da bomb for testing your sites: you’ll see exactly what you’d see on a remote (production) server. I’ve already outlined how to configure Apache for multiple web sites so refer to that to learn how to add multiple domains to your apache setup.
While you could make "google.com" or "strawberries" point to a website on your own computer I wouldn’t do that (though, for practical jokes, it has potential). If you override an existing site you’ll never be able to visit that site. What I always do is "project-name".localhost. This makes it clear that i’m working, well, locally.
Oh, don’t forget to add this to your Dreamweaver site’s setup, too. Doing this will allow you to hit F12 and view your site.
Enough jibberjabber, do it.
Your hosts file may be one of the few files that Windows uses that has no extension. Yep, nothing past the dot in the file name — oh, and there is no dot. So Unix-y. It’s located here (Windows 2K):
(I know this is stupid, but you did make a backup didn’t you? You did? Good. If not, copy, paste, rename "hosts.bak.txt". OK, you have a backup? Proceed)
Since it has no file extension –and if you added one it will break — you can’t just double-click on the file to edit. Instead do this: launch notepad (type "notepad" from Start -> Run on your Windows task bar). Next drag the hosts file into notepad and add whatever fake domains you’ll be using (note: don’t forget to add them to your apache http.conf file and then restart apache).
# ------------------------------------------ # from my Hosts file: 127.0.0.1 pizza.localhost 127.0.0.1 bf.localhost 127.0.0.1 scag.localhost 127.0.0.1 sandbox.localhost 127.0.0.1 flip.localhost #127.0.0.1 dev.localhost 127.0.0.1 wordpress.localhost 127.0.0.1 simple.localhost 127.0.0.1 phoenix.localhost 127.0.0.1 bless.localhost 127.0.0.1 text.localhost 127.0.0.1 css.localhost
What we’ve done is told windows that whenever a network request for, say, "pizza.localhost" comes through look for it on the computer with IP address 127.0.0.1 — which is your computer. Always.