April 12th, 2009
Window’s Host File Gives You Your Own Personal DNS Settings
So here’s a useful trick I was reminded of this past week.
Sometimes as a developer I write code that appears on multiple web sites. I just write a few lines that check what domain name the current site is using and execute the relevant code appropriately. Confusing? Think about when you sign up for an account at Wordpress.com. You have a blog at yourusername.wordpress.com. Everyone’s different blogs are all pointing to the same code, but different database records are pulled based on the domain name being used.
The inconvenience here as a developer is when you need to test what you’ve written with a particular domain. You can update the DNS to point to your server, but if there’s a problem then the whole world is going to be looking at the broken site. You need to see it, though, so you can fix it.
There’s a file on Windows computer that for all practical purposes lets you create your own personal DNS settings that only affect your machine. It’s called the hosts file. On a default XP or Vista installation, it’s located at C:\WINDOWS\system32\drivers\etc\hosts.
Interesting bit of trivia, this file is where the value for localhost is set.
For my purposes, though, I can use this file to point a domain name to a test server and verify exactly how a site is going to work before I change any DNS values.



