Using your HOSTS file to preview websites


Sometimes you will want to preview your website before changing DNS entries or before pointing to a new service provider. This article will outline a method which involves changing the local HOSTS file on your local machine. Your local machine will look at the entries in the HOSTS file before trying to lookup DNS information for your domain. By changing the HOSTS file we are essentially pushing the DNS entry we want onto the domain, but for your local machine only.

Since this does involve modifying configuration files on your local machine it is adviced that it is on done by experienced users. If you are not sure or at all uncomfortable about changing local files then rather ask for assistance first.

NB: remember to revert changes back after testing.

It is important to note that while you will be able to preview your website, certain features may not be available.

Windows

1. First you need to find the HOSTS file on your local machine.

  • For older versions of Windows (95/98/ME) it will normally be found here: C:\windows\hosts
  • Newer versions of Windows will have it here: C:\windows\system32\drivers\etc\hosts

2. Open the HOSTS file with a text editor with administrator rights.

  • Right-click your text editor (such as notepad) and select 'Run as Administrator'
  • The click 'File' > 'Open' and browse to your HOSTS found in (1)
  • Save a backup of the original by selecting 'Save As' and renaming the copy to HOSTS_original or something that you can easily identify if you need to at a later stage.

3. Add additional lines for your domain.

  • The HOSTS file contains IP addresses and their corresponding host names, e.g. 127.0.0.1 localhost.
  • You can add additional entries for your domain below that. You will first type the IP address of your new server or host and then the domain name. As an example the HOSTS file should now look like this:
    127.0.0.1 localhost
    92.168.23.18 yourdomain.com
    92.168.23.18 www.yourdomain.com 

4. Save the changes made to the HOSTS file and restart any open browsers.

5. If you need to flush your DNS cache.

  • Open the command prompt
  • For older versions of windows this can be found by selected 'Start' > 'Run' then type: cmd 
  • For newer versions select the start icon and type cmd in the search bar.
  • Once the command prompt is open type: ipconfig /flushdns

6. Now try browsing to the site you added to the HOSTS file

 

NB: remember to revert changes back after testing.


Mac OS X

As previously stated this involves changing local files and is only recommend for experienced users. In the case of the Mac OS we will be using Terminal and VI (text editor) so users should be familiar with these two before trying to make any changes.

1. First we need to open Terminal. This can be found in the Utilities folder under Applications if you do not have a shortcut for it.

2. Make a backup of the existing HOSTS file.

  • On the terminal line type: sudo cp /private/etc/hosts /private/etc/hosts-original
  • If prompted for enter your user password.

3. Edit the HOSTS file and add additional line for your domain.

  • On the terminal line type: sudo vi /private/etc/hosts
  • If prompted for enter your user password.
  • The contents of the hosts file should look something like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
  • At the bottom of the file add additional lines for your domain. Remember to add the IP address of the new server first then the domain name. It should then look like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
92.168.23.18 yourdomain.com
92.168.23.18 www.yourdomain.com
  • Once you have added the additional lines save and exit (:wq).

4. If you run into any permission errors along the way you may not be running terminal as the root user. You can enable this if it isn't already but going to the 'Help' menu and searching for the wood 'root'. You will see an article for 'Enabling the root user'

5. Restart any open browsers and flush you DNS cache if you need to.

  • To flush your DNS cache enter the following line in terminal: dscacheutil -flushcache

6. Now try browsing to your domain you added to the HOSTS file.

 

NB: remember to revert changes back after testing.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How do I move my site from my old host to your servers?

Transferring your account to us can be an easy process with zero downtime if done properly. To do...

Do you have curl installed in your php?

Yes. Sorry for the short answer.

Can I run a phpbb forum (message board) on my site?

Absolutely! In fact we've pre-installed it for you. Phpbb is free, open source software for...

Do you actively monitor and manage your network 24/7?

Yes we do. We monitor our network on site and remotely on a constantly... if something happens to...