close
Havoc Inspired Havoc Inspired
Search
Edit your hosts file to test your website before it goes live

This is a little trick I’ve picked up that helped me out quite a bit, especially when developing Wordpress sites where you need to get the paths and URLs right for everything to work properly.

The scenario

You’re hired to do the redesign of an existing website (example.com). Your client wants to move their website to your hosting (or new hosting) when the redesign goes live. You’ve agreed on a seamless transition from old to new on one sunny Monday morning in a months time.

So you get to work on the site in your chosen development area, demo it to the client, they’re happy and you transfer the site to the newly configured hosting space on your server, however the name servers for example.com haven’t been changed yet, it’s only Friday and the redesign isn’t going live until Monday, but you’re a contentious developer and you want to test the new design to ensure everything is working as expected in the live environment.

What do you do?

Our goal

We’re going to edit the hosts file on our Mac to make our servers IP address (e.g. 70.26.55.80) resolve to example.com, just like what a DNS server does when example.com’s name servers have been changed to point to the new hosting.

Configuring the server

All server configurations are different but the principle is generally the same, we need to find out/set our example.com to appear in the web browser when we visit our example IP of 70.26.55.80. For this example I’m going to explain the process on a Plesk driven server (from this you should be able to get an idea of how this would be achieved through other management software packages).

  • Configure your web space for example.com and upload all your files to it
  • Login to Plesk
  • Select Server from the left-hand menu
  • Go to the IP Addresses – This should present you with a list of IP addresses assigned to your server, when you configured the hosting for example.com you will have selected one of these IP addresses (you may even only have one IP address in this list)
  • For each IP address there’s a column titled Hosting with a link showing the number of domains assigned to that IP address, click the link that corresponds with the IP address you selected for the web space
  • Select example.com and click Set As Default - You can only select one default domain to be displayed if you visit that IP address in a browser
  • Make a note of the IP address (e.g. 70.26.55.80)
  • Visit 70.26.55.80 in a web browser – You should be presented with your newly uploaded site for example.com

Editing the hosts file

Mac’s have a hosts file that we can edit to make 70.26.55.80 resolve to example.com just as if the name servers for example.com had already been changed (obviously this only effects the Mac you are on).

I’m sure you’ll be able to do this on a PC as well but don’t ask me how, I’m a Mac man.

  • Open a Terminal
  • Enter sudo vim /etc/hosts then enter your administrator password

This will open the hosts file in the vim editor. You should see 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

To edit the file press ‘a’, cursor down to the bottom of the list and enter:

70.26.55.80       example.com

Press ‘esc’ then enter ‘:wp’ to write and quit vim.

If you now visit example.com in your web browser it should resolve to 70.26.55.80 (the new hosting) and you can test your newly designed site in its live environment.

Now all that has to be done to make the site seamless go live on Monday is change the name servers for example.com to point to your hosting and you can guarantee that everything will work.

Important note: Remember to take the entry for example.com out of your hosts file when you have finished testing, otherwise you may end up seeing a false representation of what is actually being displayed at that domain when the name servers are changed!

Final Tip: Instead of removing the line completely from your hosts file simply add a # to the beginning of the line. This “comments out” the line and stops it from being applied. If this is a process that you are going to use regularly in the future then the line is already in your hosts file and you can simply remove the # and change the domain name when you need to.

arrow5 Comments

  1. 6 mos, 1 wk ago

    Great post, thanks!

    The host file on Windows machines is located at c:\[windows directory]\drivers\system32\etc\hosts

    I’m a Mac man as well but if you’re like me and try to avoid Vi or Vim at all costs you might well be a TextMate user. If you are instead of typing:

    sudo vim /etc/hosts

    you can just type:

    mate /etc/hosts

    That’ll open a new TextMate window and prompt you for your system password in a nice friendly OS X dialogue box :)

  2. Shaun Hare
    6 mos, 1 wk ago

    Yes a trick I use a lot too Ryan, although I am a MAC man too for windows users the file is located in

    C:\windows\system32\drivers\etc and the same applies, on my windows machine I just drop a shortcut on the desktop to edit the file in notepad.exe

    e.g. start notepad.exe C:\windows\system32\drivers\etc\hosts

    Another tip to remember is make sure after a change you quit any open browsers before you try and test as otherwise sometimes the desired location is not picked up

  3. 6 mos, 1 wk ago

    If you’re a PC user, then it’s a similar procedure.

    The hosts file is usually located at:
    C:\WINDOWS\system32\drivers\etc

    Simply open that in notepad and add the same entries as described by Ryan. You may need to restart your browser (not the computer) after you’ve made the changes.

  4. 6 mos, 1 wk ago

    Thanks for the details guys. I’m sure some dirty PC user will find it useful! :-P

  5. Dirty PC User
    1 min ago

    Thanks guys, It was actually useful. Have to use windows at work. Otherwise its linux!!

Leave A Comment