How To Edit Local Host For Testing Web Sites

How To Edit Local Host For Testing Web Sites

flowchartThere are times when you may wish to test a web site you have designed before the actual domain name’s DNS (Domain Name System) entry is updated.

DNS Explained

Think of yourself placing a phone call to the 411 Operator. You ask the operator “What’s the phone number of Joe’s Pizza in Paramus, New Jersey?” The Operator looks through all the company listings in that area and finds the listing for Joe’s Pizza. The Operator replies “The phone number for Joe’s Pizza in Paramus New Jersey is 201.983.7564. I will connect you now…” and the next thing you know you are speaking with Joe’s Pizza ordering your dinner.

The Internet is the same way for the most part. Just like every company out there will have a different and unique phone number, every web site out of the Internet will have a specific numeric address assigned to it known as an IP address or “Internet Protocol Address”. Most people do not realize that Internet servers can only be addressed via IP addresses. However it would be impossible to remember that the IP Address for Joe’s Pizza is 201.983.756.4, so the Domain Name System was created.

Host names are the web site addresses you see every day: www.google.com, www,joespizza.com, and so on. We used these “words” like www.google.com so humans don’t need to remember the long IP address numbers when they want to visit a web site.

Now let’s use the first example of placing a traditional phone call into the context of visiting a web site using your computer’s browser to explain how web addresses work.

You happen to know that the web site address for your favorite pizza joint is www.joespizza.com (because that’s a lot easier to remember than some four part numerical value). You type “joespizza.com” into your web browser. When you hit the Enter key, your request is sent to the “Internet 411 Operator” used by your Internet Service Provider company (or ISP for short). In the world of the Internet, this “411” Operator is know as a Domain Name Server. This Domain Name Server (DNS) looks through all its domain name entries for Joe’s Pizza. The DNS Server is thinking “The web site JOESPIZZA in the .COM domain is being hosted by a web server with the IP Address of 201.983.756.4” and forwards you to that location and before you know it, you are looking at the web site for Joe’s Pizza.

What is the Hosts File On My Computer?

Simply put, the Hosts file is similar to an address book. Exactly like the example above, when you type an address like www.joespizza.com into your web browser, the Hosts file on your own computer is referenced to see if you have the IP address or “telephone number” for that web site. If you do, then your computer will use that number it has on file locally to “call” and the corresponding web site opens. If not, your computer will ask the DNS Server belonging to your Internet Service Provider for the associated IP address for the corresponding web site and connects you to that web site. The majority of the time, you will not have all the IP Addresses of all the web sites from the entire Internet in your “address book”. You will probably have very few (if any) entries within your Local Hosts file. Therefore, most of the time your computer will ask for the IP addresses of web sites you wish to visit from your ISP.

Why Would I Want to Edit The Host Files On My Computer?

Sometimes when designing a new web site, you may need to test certain aspects of the site before launching the site live to the general public. By editing your local Hosts file, you can affect what happens when you type in a certain web site address on your own system by redirecting the web browsers on your computer to a different IP address to view that particular site than the rest of the world would see. So while the rest of the users of the Internet type “www.joespizza.com” into their web browsers and get redirected to the web server at the IP address 201.983.756.4, visiting the same “www.joespizza.com” web address on YOUR own computer only could bring you to the IP address of say 131.34.23.5 (your testing web server, for example). Once you are done testing your site, you could then edit your local Host file again to connect to the site as a regular Internet user would.

To test your web site using your own domain name BEFORE DNS propagation has completed, you can use your local computer’s HOSTS file. Your computer will use the entries in your HOSTS file FIRST before it tries to use your IPS to look up the DNS information for your domain.

REMEMBER: When you are finished testing, remember to remove the custom lines that you added to your Hosts file.

How to Edit Your Hosts File on a Windows PC (Windows 95/98/Me/2000/XP/2003/Vista/7)

Let us assume for this example your testing server has an IP address 88.46.57.157 and you wish to visit that server when you type “http://example.com” into a web browser BUT still wish to still see the site as the rest of World Wide Web does when you enter “http://www.example.com” into your browser instead.

  1. Launch NotePad
    Click on your Windows icon in the Task Bar in the bottom left hand corner of your screen.
    In the “search” field, type “Notepad”. A list of entries will appear.
    IMPORTANT (Windows 7 and later): Right-click on Notepad choice found and select “Run as Administrator “. If you don’t have the run as Administrator option it means you don’t have these privileges. Contact your IT person to get them or to set up the local host for you.
  2. Open your local HOST file
    Within Notepad go to File Menu->Open and locate the HOSTS file on your computer.IMPORTANT: In the bottom right hand corner of the Open dialog box, change the popupmenu FROM “Text Documents (*.txt)” TO “All Files”
    Typically it is in one of the following locations:

    • Windows NT/2000/XP/2003/Vista/7/8/10 – C:windows/system32/drivers/etc/hosts
    • Windows 95/98/Me – C:windowshosts
    • Consider performing a “Save As” so you have an original copy of the file that you can restore later.
    • Filename: hosts127.0.0.1 localhost

      You can add additional lines to this file that will point requests for a particular domain to your new server’s IP address.

      Example:


      Filename: hosts

      127.0.0.1 localhost
      88.46.57.157 example.com

      Edit to local HOST File
      You will see two columns of information, the first containing IP addresses and the second containing host names. By default, a windows hosts file should be similar to the following:

  3. Save your changes
    IMPORTANT: be sure to save as a host file, NOT as a text (.txt) file
    Windows wants to save it as text (.txt) so you need to

    1. Change “save as” type to “all files”, and then
    2. Click on “host”  (the original file).
  4. Restart any currently open browsers
  5. Flush your DNS Cache
    You may also want to flush your DNS cache. In Windows, go to Start/Windows button in the bottom left of your Task Bar, and in the search field type “cmd” and hit the Enter key on your keyboard.
    Type the following: ipconfig /flushdns
  6. Test your changes
    In your web browser you should see your site as it appears on your testing server when typing http://example.com/ but still be able to see the site on its current web server by visiting http://www.example.com/

How to Edit Your Hosts File on an Apple Macintosh Using Mac OS X

Let us assume for this example your testing server has an IP address 88.46.57.157 and you wish to visit that server when you type “http://example.com” into a web browser BUT still wish to still see the site as the rest of World Wide Web does when you enter “http://www.example.com” into your browser instead.

  1. Open Terminal, which is in Applications, then the Utilities folder. To do this go to the Finder (Desktop) and from the main main bar at the top of the screen choose “Go” and then “Utilities”. Find the Terminal application icon and double click.
  2. You may want to first make a backup copy of your existing hosts file:
    sudo cp /private/etc/hosts /private/etc/hosts-orig

    Enter your user password at the prompt. Then type the following command to edit your hosts file:

    sudo nano /private/etc/hosts

    Enter your user password at the prompt if asked.

  3. You will see a file with contents similar to the following:

    Filename: hosts

    ##

    # 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

    Using the arrow keys on your keyboard, navigate around this file and add your domain and IP address to the bottom of the file. For example:


    Filename: hosts

    ### Host Database## localhost is used to configure the loopback interface# when the system is booting. Do not change this entry.##127.0.0.1 localhost255.255.255.255 broadcasthost::1 localhostfe80::1%lo0 localhost

    88.46.57.157 example.com


  4. When done editing the hosts file, press the keyboard combination Control+O to save the file.
    Next, press the Enter key to confirm the Save operation. Finally press the keyboard combination Control-X to exit the editor. You may also need to grant yourself sudo priveleges, if you got a permission error in Step 2. In your “Help” menu, search for “root” and select the instructions for “Enabling the root user.” Follow those instructions.
  5. You may also want to flush your DNS cache.
    Type the following command into your Terminal window: dscacheutil -flushcache then hit the Enter key.
  6. Restart any currently open browsers.
  7. In your web browser you should now see your site as it appears on your testing server when typing http://example.com but still be able to see the site on its current web server by visiting http://www.example.com

Share this post