If you are having problems with the web browser loading content or showing pop-up ads from a web site, it's possible to block that web site using the Windows HOSTS file. The blocking can be done by adding a custom rule in the HOSTS file, that prevents the browser from translating the unwanted site's domain name to an IP address. If this sounds complicated, fear not, all you have to is to add a short snippet of text in a text file.
So let's get started. You can find the step-by-step guide below on how to edit the HOSTS file.
Press Windows + E on your keyboard. This will start Windows File Explorer.
Locate C: in the left pane.
In the left pane, browse to C:\Windows\System32\
Locate cmd.exe in the right pane.
Right-click cmd.exe and choose Run as administrator. If Run as administrator is missing, you are probably running an older version of Windows, such as XP. In that case, choose Open instead.
The User Account Control dialog asks you if it's OK to run cmd.exe in Administrator mode. Click Yes. Administrator mode is required to edit the Windows HOSTS file. If you are using an older version of Windows, such as XP, there's no UAC dialog.
A command line window will open up.
In the command line window, type in or copy/paste the following three commands. Press ENTER after each line.
cd %WINDIR%\System32\drivers\etc copy hosts hosts_backup notepad.exe hosts
The first line changes the working directory to where the HOSTS files is located. The second command creates a copy of the hosts file named hosts_backup. Always good to have a backup, right? The third command starts the Notepad text editor that allows you to edit the HOSTS file.
Now, let's block the unwanted web site. Let's assume the web site is strk.aigames.mobi which is known for showing lots of unwanted pop-up ads. In the HOSTS file, scroll down to the bottom and enter:
You need to replace strk.aigames.mobi with the site that you want to block.
So, why does adding 127.0.0.1 strk.aigames.mobi block the strk.aigames.mobi web site? When the browser is about load some content from strk.aigames.mobi, it asks the operating system for an IP address. The OS will first look in the HOSTS file for a domain -> IP mapping. And since there is one, the browser will use the 127.0.0.1 IP address. 127.0.0.1 is a "loopback" address to your machine. That is, the browser will connect to your machine, instead of the real strk.aigames.mobi web server.
Save the HOSTS file from Notepad's File menu. If you see an error message saying:
You don’t have permission to save in this location. Contact the administrator to obtain permission.
Then you are not running Notepad as Administrator. Please check out the instructions above to use Notepad as admin.
Restart your machine.
That's it! Did that solve the problem you were experiencing? If not, you probably need a sligthly more advanced approach to block the unwanted site. Please read on.
I'll show how to use the web browser's network log to find the sites that you want to block. This is a real example from one of my lab machines where there were lots of pop-ups ads. There were also injected ads while browsing sites that should not show any ads what so ever. To monitor the network connections that Firefox did, I used an add-on called Live Http Headers. Once installed you can open it from Firefox' Tools menu. You can also get the same information from the Network tab in Firefox' Developer Tools. If you are using Google Chrome, you can use the Network Tab in Chrome's Developer Tools.
Here's how I stopped the injected ads and pop-ups:
First I launched Firefox and opened the Live Http Headers dialog. I cleared the current data in the log by clicking the Clear button in the Live Http Headers dialog.
Then I typed in example.com in the browser's address bar. Example.com is useful since it does not contain any ads and does not load any content from any other site. However, as you can see in the screenshot from my machine, there's some injected ad-related content. And in the network log you can also see that it loads some ad content from engowe.com.
So I added 127.0.0.1 engowe.com to the HOSTS file, reloaded the example.com web page, and repeated 1, 2 and 3, until I did not see any ad-related domains being listed in the network log. These are the additions I did to the HOSTS file:
Thank you very much for reading! Hope this guide helped you block some unwanted site.