Development, Tutorials

Preview a domain by editing your host file

When developing a new website it can be very useful to look at the site on a live server before you point the domain to it, this way you can check out the site before it goes live, thankfully this is very easy to do using a host file that's located on your computer.

How it works is you add the server IP address followed by the domain name then when going to the domain name instead of the usually IP address being used, your computer will use the IP address you specified in the host file.

Once the site is ready point the domain to the server isn the usual way, change name server or add an A record etc at this point its safe to remove the record in the host file.

To set this up is a little different on Mac and Windows.

MAC:

open terminal and type

sudo nano /private/etc/hosts

Press enter, you may need to enter your password.

The first few lines will be comments, comments start with a # on a new line enter the IP address of the server and the domain name:

234.52.3.55 mydomain.com

Then save the file by pressing ctrl + x then press y to confirm and enter to overwrite the host file.

Windows:

Find and open the host file sytemroot is normally c:/windows

%systemroot%system32\drivers\etc

In order to be able to make changes to the file do not open the file directly instead open notepad as an administrator by clicking start then right clicking over notepad and select run as administrator 

Once notepad is open go to File - Open then navigate to the host file c:windows\system32\drivers\etc

Select the host file, once open you can write to the file once the changes are written save the file, when saving make sure the file type is set to all files.

Once the host file have been saved the changes are instant. This method makes testing a site very easy to do before the domain is pointed to the server.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.