Overriding DNS for Testing

This has come up three times in the last two weeks. There are times you want to do URL testing on a non-production test machine, but you don’t want to screw around with DNS either.

When resolving a host name, POSIX and Windows machines have a hosts file that it looks in first. If it doesn’t find the host name there, it falls back to using DNS to convert the host name to an IP address.

This can be handy when, for example, you’re monkeying around with Apache URL rewriting and want to (gasp) actually ensure it’s going to work correctly with the production servers’ URL.

For example, if the production servers are at foo.com, and the test server lives at, put the following line in the /etc/hosts file of the machine you’re running the tests from (assuming a POSIX machine): foo.com www.foo.com

After flushing the DNS cache, the test machine will now associate foo.com with the test server instead of the production servers.

To reverse the process, remove (or comment out) the line from the hosts file.


This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply