Edit the Windows Hosts file to block or redirect websites

The Windows Hosts file is a file that Windows uses to manage and assign IP addresses. By editing the Hosts file, Windows can be modified to block or redirect specific websites and even protocols used by programs and applications.

In this article, we will walk you through the steps to properly edit the HOSTS file. If you prefer to watch a video of the process, you can also visit our Youtube video†

Edit hosts file in Windows

To start editing the Windows Hosts file, you must first locate it. Open Windows Explorer and click This PC or My computer† Double click on C: and then the Windows folder and scroll down the page until you reach the System32 folder. Open in that folder drivers and then open etc† You will now see several files, one of which is hosts

hosts file

Now notice that the file type for the hosts file is displayed as File† Since there is no default program set to open a file type like this, double clicking the hosts file will just give you a Windows prompt asking which program you want to use to open the file.

Choose a program prompt - Windows 7

From this prompt, you can choose to edit the hosts file using Notepad. So, just click to select Notepad and click on the Okay knob. From there, Notepad will launch with the hosts file information.

hosts file notepad

This way of opening the hosts file was demonstrated to show where the hosts file is actually located in Windows, but you cannot edit it because it is a system file. To edit the file, you must first open Notepad as an administrator.

click on Get started and type in Notepad, but do not click Notepad to open it. rather, right click the Notepad list to open the context menu. Select the option: Run as administrator

run notepad as administrator

With Notepad open, select File > Open† Navigate to C:WindowsSystem32driversetc† You will get a blank screen with the prompt No items match your search† Change Text documents (*.txt) to all files using the drop-down menu. Now you can use the hosts file and click Open

open hosts file

Adding files to the hosts file is very easy. The hosts file uses the format:

IP Address   exampledomain.com

Block websites with HOSTS file

Blocking a website in Windows is as easy as typing the following into the bottom of the hosts file:

127.0.0.1    www.exampledomain.com

So if I wanted to block a website like www.nytimes.com I could just add the following line:

127.0.0.1    www.nytimes.com
redirect website hosts

Basically what we’re telling Windows is that the www.nytimes.com website should redirect to the IP address 127.0.0.1, which is just the loopback address on our local system. If you have not set up a local website on your computer, you will only get an error page in your web browser.

site cannot be reached

Pretty cool, huh!? You can of course see how this can be used in different ways: a joke, parental controls, etc. If you don’t want to block the website that way, you can also redirect it to another website. To do this, you must first find the IP address of the other site.

To do that, just open a command prompt (click on Start and type in CMD) and type the following command:

ping examplewebsite.com
ping website

In my example, I ping Adobe.com. The IP address is 192.150.16.117. Now I can just put that number in my hosts file for www.nytimes.com.

hosts file redirection

Now when I visit www.nytimes.com I am redirected to Adobe.com! Nice! Note that if this doesn’t work for the websites you enter, it could be because of the URL you’re using. For example, it makes a difference whether you www.nytimes.com unlike nytimes.com without the www† Visit the website and see exactly what the URL of the website you want to redirect is. Always try without www first to see if that works.

If the website uses HTTPS like Google.com or something, it should still redirect if you use the hostname. There is no way to specify the HTTPS version of a website in the HOSTS file, but it should redirect the HTTPS and non-HTTPS versions of the website if you just use the hostname (e.g. google.com ).

Finally, you can use the hosts file to create simple shortcuts to your own devices on the network. For example, my router is up 192.168.1.3 on my home network, but I could add the following line to my hosts file and just type: myrouter.com in my address bar.

redirect to local device

It doesn’t really matter if myrouter.com is a website or not because it reads the hosts file first and redirects you to the IP address specified in the file. It’s worth noting that not all browsers can use the hosts file, so if it doesn’t work, that could be the problem. I tested it with IE, Microsoft Edge, Chrome and Firefox and it worked on all browsers.

In general, the hosts file is still useful even in Windows 10. It also still works fine in Windows 8, 7, Vista, etc. If you have any questions, feel free to comment. Enjoying!



Post views:
16

Leave a Reply

Your email address will not be published.