How To Block a Web Site with the HOSTS File

Example of a pop-up window.

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.

  1. Press Windows + E on your keyboard. This will start Windows File Explorer.

    Windows + E starts Windows File Explorer

  2. Locate C: in the left pane.

    C: is located in the left pane in Windows File Explorer

  3. In the left pane, browse to C:\Windows\System32\

  4. Locate cmd.exe in the right pane.

  5. 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.

    Right-click to run cmd.exe as Administrator. Cmd.exe is located in the System32 directory.

  6. 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.

    The UAC dialog will pop up, asking you if it's ok to run cmd.exe in elevated mode.

  7. A command line window will open up.

    The cmd.exe window will pop-up.

  8. 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.

    The HOSTS file in Notepad.

  9. 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:

    127.0.0.1 strk.aigames.mobi

    You need to replace strk.aigames.mobi with the site that you want to block.

    How to block a site by directing it to 127.0.0.1 in the HOSTS file.

    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.

  10. 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.

  11. 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.

Using the Network Log to Block Sites

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:

  1. 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.

  2. 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.

    Injected ads on example.com and a network log.

  3. 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:

    The resulting HOSTS file which blocked the pop-ups and injected ads.

Thank you very much for reading! Hope this guide helped you block some unwanted site.

Comments

Bill Jacobs writes

1 thumb

pixel.adsafeprotected.com was driving me mad for a UK Crossword puzzle in the daily Express. I'd get half way through then the slowdown of input/response became so ridiculous that I had to restart the puzzle. Anyway, blocking this damn thing in the hosts file has restored my enjoyment of doing this puzzle to completion now. Thanks again. About 3 months of pain gone.

# 19 Oct 2016, 3:32

Roger Karlsson writes

0 thumbs

@Bill: Thank you for sharing! Happy to hear that blocking the site solved the problem.

# 20 Oct 2016, 0:58

Leave a reply