The Internet in itself is not a very safe place to be in with viruses, torjans, adware and malware all waiting to attack your computer. You can stay safe from these using Antivirus softwares or Anti spyware softwares. To top that list there are tons of sites which popup unwanted ads on you to make your surfing experience a horrible one.
To overcome that there are several softwares and addons to block those ads but those solutions are rather limited to a single browser and the next time you change your PC you will have to reinstall those softwares.
Instead of using multiple softwares for multiple programs to block advertisements you can use a simple trick that will block those ads out without the need for installing any softwares.
The Hosts File
In Windows there is a file called hosts using which you can control the mapping of network names to IP addresses. You can call it as a local DNS cache which windows checks first before using the DNS servers provided by your ISP.
So in short you can control the DNS cache for your system using the hosts file. Let’s put it as such you may not want to load a NSFW (not safe for work) site on your office PC and you just want to make sure it does not load the site at all you can use the hosts file to redirect the NSFW site to say google.com, here is how you can do that.
Locate and open the hosts file
The host file can be located on a Windows XP or Windows Vista machine by going to C:\Windows\System32\Drivers\etc folder. The hosts file does not have any extension and is read only so you will have to change the file permissions before editing it. (Remember to change back the permission to read only once you have edited it so that malicious code can not write to it)
If this is the first time you are opening the hosts file then it should look like the screen shot below
Add a DNS entry to map a IP to a server
In the file as you can the first parameter is 127.0.0.1 which is the IP address of your local machine and localhost is the name of the server, so for instance if you type in http://localhost/ on your machine it will redirect you internally to http://127.0.0.1. So now you can simply enter another entry to block your NSFW website and point it out to go to 127.0.0.1 by adding an entry like this.
Once you have done this close the hosts file and try loading the domain in your browser you will internally get redirected to http://127.0.0.1 and your site will not load. If you have a webserver running on port 80 you will see the welcome page for that instead of your site.
If you are a bit overambitious and want the NSFW site to go to Google.com or any other site you want, you can use this simple trick. Open cmd and type in tracert www.yoursite.com. Once you issue this command you will see some packets being transferred which traces the route, we are not interested in that though. Just after you issue the command you will see the IP Address of the site you are trying to trace, copy that and replace the 127.0.0.1 in the above example with the new IP Address.
For this example I added a entry on my hosts file to point www.yahoo.com URL to the Google IP and when I open the browser and type in www.yahoo.com here is what I see when I load www.yahoo.com in the browser.
This setting is global and any browser or application that tries to access www.yahoo.com will get the Google Page in response. If you want to restore normalcy simply delete the entry you added from the hosts file.
You can use the trick to play fun on a colleague and redirect their favorite sites to some other site, you can use it for endless different things but the main purpose of this should be to block unwanted sites that pop up ads or are not safe for children and so on.
Since this trick does not require you to install any software you can create and store a hosts file on a portable drive and use it where ever you want.
I will definitely suggest you to use a comprehensive list of sites to block created by MVPs.org and pre configured to the hosts file. Just download and copy the file to the etc folder or simply use the bat file included to do it automatically for you and you can start a ad free Internet experience without having to install any softwares.