Register / Log in
26
October

Hosts files are a handy thing that every web developer should take advantage of.

Here’s the setting: you have a web site, for facespace.com, that you’re developing on your local. It references something from, say, mybook.com, which is another site of yours, but one which resides at a different domain. Let’s say that facespace.com/index.htm references mybook.com/logo.jpg. A discussion about the principles of good design put aside, if you’re developing and testing on your local, this poses a problem. The problem is that you can’t simply use local references, as the files are shared across domains. It seems that to test on your local, you need change all of the path names – not the desired approach.

A hosts file redirects requests to a particular domain to a specified ip address. You can ensure that references to both facespace.com and mybook.com to resolve to your local. In addition to just local development and testing, this is great if you have a version of the company site running on your local, and you’d like me to check it out. Problem is, I’m in a different office. I can set my hosts file so that references to the company site are redirected to your local, and by going to facespace.com I can see the changes that you’ve made. This is also great when the site is deployed to a quality assurance server. It’s also great for pranks.

The hosts file, in both Linux and Windows, is a text file. To edit the file, you must have administrator permissions. Often times, this will be the case be default on Windows. If you’re accessing the file from the command prompt in Linux, try prefixing your command with sudo. The locations are as follows:

Each entry is one line containing the target IP, one or more spaces, and the domain that is to be redirected. For example, pinging google.com and bing.com reveals

If you open up your hosts file and enter the following, typing “bing.com” in a web browser will take you to Google’s website, and typing “google.com” will take you to Bing’s site.

# Comments start with a hash sign
# You should also probably have localhost in the hosts file - though, it usually works fine without it.

127.0.0.1 localhost
74.125.53.100   bing.com
64.4.8.147        google.com

After switching hosts file settings, you can check to ensure that the changes have taken place by pinging any of the domains and observing what ip is resolved. Additionally, even if everything in ping looks okay, you have to close and re-open any browser you had going at the time of the switch for the changes to take affect. Also, if you’re running the tool fiddler, make sure to close and re-open that. (If you don’t now what fiddler is, don’t worry, you’re not running it.)

So, in recap, tweaking the hosts files is great for web developers developing locally, on a testing server, or playing pranks around the office. (switch gmail.com with hotmail.com, or write a javascript to take them to gmail.com 90% of the time, and a totally wack site 10% of the time). Oh, and as one final note, sometimes when your web browser has been hijacked, look here for rogue entries!

View Comments

blog comments powered by Disqus