Tutorial Editing your local hosts file to preview sites

How to edit your local hosts file to test websites
For a long time now Apache's mod_userdir has been the preferred way to check if sites are working properly on a machine without DNS being configured. This allows users to confirm a site is functional before the DNS is pointed to that system, allowing the site to be tested before it goes live.

However, there are two reasons that mod_userdir may no longer be the best option for testing websites: absolute links and new PHP handlers.

With the rise of Search Engine Optimization and content scraping, developers are commonly using absolute paths for all site content. This makes it impossible to test a site with mod_userdir - although the homepage of the site will work any subsequent links you click will follow the full URL, bringing active DNS into the equation, and taking you to the live site.

With EasyApache 4 providing more control than ever over the PHP configurations on your system there are also new PHP handlers to be aware of. Specifically, the PHP-FPM and CGI handlers don't work with mod_userdir.

So what is the best way to test sites in these conditions? The answer is by editing your local computer's hosts file.

While this does take a bit more effort at the user level than just entering a slightly different URL into the browser, editing your local hosts file to preview sites often results in a better preview experience and may be the only option available if you're running one of the PHP handlers where mod_userdir isn't supported. Editing your local computer's hosts file is a way to trick your computer into thinking a website is hosted on a different machine than it really is by directing the DNS to a different location for a specific domain. This is something that can be configured quickly on any operating system, and we'll be covering the steps necessary to do this in Windows, Mac OS, and Linux.

Editing the hosts file
In all the examples below we'll be pointing our imaginary website,, to our testing IP of

It's important to note that no matter which version of Windows you're working with you'll need to have administrator access to the machine to change the file

Windows 7
On Windows systems the hosts file is located in C:\windows\system32\drivers\etc\hosts - you can navigate directly to that file through Windows Explorer or you can enter this in the search box when you click the Windows icon:

notepad c:\windows\system32\drivers\etc\hosts

That will open the hosts file in Notepad which will look like this:


At that point you can add this line of text to the bottom of the file:
Just save the file, restart your browser, and your site will be pointed to that new location.

Windows 8, 8.1, and 10
In Windows 8 and 10 you'll need to open the Notepad app as an administrator. You can do this by searching for the application, right clicking, and choosing the "Run as Administrator" option.
Once Notepad is running click File, Open, and then enter the following line into the "File name" box:


Now you can edit the file normally and add this line of text to the end of the file:

Just save the file, restart your favorite browser, and now you're ready to view the site on the new IP address.

Mac OS
The easiest way to perform this work in the Mac OS is to access the file using terminal. You'll want to run this command to open your hosts file. In our example we're using the Vim text editor, but feel free to use any editor you like.

sudo vim /etc/hosts
This will open up the hosts file on your system, which will look something like this:


At the bottom of the file you'll want to add this line to point the site to the new IP address:
Once you've added that line, just save the file. You'll also need to restart any browser's you're using to make sure that change is loaded.

The process of editing the hosts file in Linux is nearly identical to the MacOS process, although you may not see those commented lines at the top. Here is what the /etc/hosts file from a CentOS system looks like:


Just like in MacOS you'll open this file as an administrator with your favorite text editor, add the line to the bottom of the file, save the file, and restart your browser to load the changes.

Contributed by: @cPRex
First release
Last update
0.00 star(s) 0 ratings