What is the Windows “Hosts” file, and how do you get the hosts file to work properly? Windows Hosts file not working? Problem solved.

When you type a domain name into a browser and press Go, one of the first things a Windows PC does is check the “hosts” file to see if there is anything relevant to that domain in it. If not, then it will use the normal IP address it finds from whatever DNS server list your PC is using. However, if it finds a domain name in the hosts file, it will bypass all that and go straight to the specified IP address to try and find the website there instead. The Hosts file is a type of text file. It is normally empty, but you can modify it to force the computer to look at a very specific IP address for domains, ignoring the Internet-wide DNS (domain name system) settings.

Why might you want to do this? Perhaps you are migrating a website from one host to another and want to check the website is working properly before changing all the Domain’s settings.

If you are reading this, you probably know all that already and are just wondering how on earth to actually get the hosts file to work properly!I found figuring this out to be immensely frustrating, but I got there in the end and it now works every time. I’m going to describe the process, as it works on my standard Windows 10 (pro) computer.

Firstly, a note on checking success. If the hosts file changes are done correctly, you should see the results immediately. You won’t have to flush DNS, or reboot your browser (at least with Chrome), or reboot your PC, or any of that stuff. Firing up a command prompt and “ping domain.name” will show you which IP is being used. It should match what you put in the hosts file.

Here are the summary steps to get a working hosts file

  1. Be logged in as a normal admin user.
  2. Create a new hosts file somewhere like the Desktop or Documents.
  3. Edit (using the correct format) and save the hosts file with Windows Notepad.
  4. Go to the real hosts file location and rename it.
  5. Copy and paste the new hosts file to that location, accepting the permissions popup.
  6. Rejoice

Here are the details

If you only have one Windows account on your PC, it’s pretty certain that it’s an admin.

Create a new text file in one of your normal folders (NOT in the real hosts file location – that will definitely not work). Call the file “hosts.txt”. I suggest you keep it somewhere useful for later so you can easily modify it next time you need it. Rename the file to remove the .txt file extension – Windows will warn you it might stop working, but accept anyway. If you can’t see the file extensions, then in File Explorer, click view > options > view (tab), and uncheck the “Hide extensions for known file types” box.

Right-click on the new hosts file and choose the “Open with” option. Your aim is to open it with Windows Notepad. Not any other sort of advanced notepad app that you might have installed. Just Windows Notepad. You shouldn’t need to open it as an admin or anything like that because you are already an admin.

Now is the time to add your server repointing information. Type the target IP address for the new server, add a single space and then type the domain name, without any “https://” and without any trailing “/”. Then do a carriage return and repeat the line above, but add a “www.” to the domain name. Finally, add a carriage return so that there is a single empty line underneath the two redirect lines. Without that final carriage return, the file will not work. You should be able to put the cursor in it, but neither move it along the line, nor be able to move the cursor down any more lines. So it might look something like:

12.13.14.15 domainname.com
12.13.14.15 www.domainname.com
MAKE SURE THIS LINE EXISTS, IS EMPTY & NO FURTHER LINES

Note that you cannot use the hosts file for https, only for http, so you’ll need to test your new server without SSL and then switch to https once happy.

Save and close the new hosts file.

Navigate to C:\Windows\System32\drivers\etc

Here you should find a hosts file – it might have some comments in it, but it doesn’t really matter what’s in it. Rename it “hostsbak”

Copy and paste your new hosts file from wherever you saved it into C:\Windows\System32\drivers\etc accepting any prompts to “continue”.

You should NOT try and paste your new file over the top of an existing hosts file – it won’t work. Make sure you do the rename step above.

It should now be working

You are now done. The hosts file should be working. Open a command prompt and ping the domain name to check which IP address is returned. If you followed these instructions exactly, it should be the one from your new hosts file. If it’s not, you either did something wrong along the way or there’s something unusual about your Windows install, in which case you are on your own.

How to turn it off again

I find that the best way to prevent the redirect is to just rename the hosts file, because then it’s easy to switch it on and off while checking sites, without needing to recreate or edit etc.

Things that normally go wrong:

  • Trying to edit the hosts file in C:\Windows\System32\drivers\etc
  • Trying to paste over the hosts file in C:\Windows\System32\drivers\etc
  • Trying to edit the new hosts file with anything other than Windows Notepad
  • Leaving the new hosts file as a text file, without removing the extension
  • Forgetting the final empty line in the hosts file
  • Expecting to be able to access an encrypted address (https://) with the hosts file
  • Performing any of this as a non admin user

If you finally succeed after reading this, please comment 🙂

Previous post
Previous post
I've experimented with a lot of different ways of improving…

Pin It on Pinterest