How to Use the Hosts File to Preview Your WordPress Site

The hosts file is a plain text file on your local computer that your operating system (OS) uses to map hostnames (like yourdomain.com) to specific IP addresses. It acts as a local, authoritative override for the global Domain Name System (DNS).

Modifying this file is crucial when you need to preview or test your WordPress site on its new server before updating your domain's official DNS records. This allows you to verify that your site works perfectly on the new hosting environment without affecting live traffic for other visitors.

Why Modify the Hosts File?

When you migrate your WordPress site to OneStopWP, there's a delay (known as DNS propagation) between the moment you update your domain's nameservers and when all internet users see the change.

By editing your hosts file, you instruct your computer to bypass the public DNS and go directly to the new server's IP address.

This is useful for:

  • Pre-launch Testing: Verifying your site works correctly on the new server before going live.
  • Migration Testing: Ensuring all database connections and files loaded properly after migrating to OneStopWP.
  • Third-party integrations: Check if all third-party tools are working properly on the new environment

How to Edit the Hosts File

The procedure for editing the hosts file varies slightly depending on your operating system. In all cases, you will need administrative (admin) access to your computer.

1. Windows Users

  1. Locate the File: The hosts file is located at: C:\Windows\System32\Drivers\etc\
  2. Open as Administrator: Right-click on your preferred text editor (like Notepad or VS Code) and select "Run as administrator."
  3. Open Hosts: From within the administrator-opened text editor, navigate to and open the hosts file.
    • (Note: You must open the editor as Administrator first, or you will not be able to save your changes.)

2. macOS (Mac OS X/OS X/macOS) Users

  1. Open Terminal: Go to ApplicationsUtilitiesTerminal.
  2. Run Command: Enter the following command and press Enter: sudo nano /private/etc/hosts
  3. Enter Password: Type your user password when prompted (the cursor will not move, which is normal).

3. Linux Users

  1. Open Terminal: Open a Terminal window.
  2. Run Command: Enter the following command and press Enter: sudo nano /etc/hosts
  3. Enter Password: Type your user password when prompted.

Adding the Server IP and Domain

Once the hosts file is open in your text editor (with admin privileges), follow these steps:

  1. Find the IP: Obtain your OneStopWP server's IP address from the hosting dashboard.
  2. Add a New Line: Scroll to the end of the file and add a new line in the following format:

xxx.xxx.xxx.xxx yourdomain.com
xxx.xxx.xxx.xxx www.yourdomain.com

  • Save and Close: Save the file (FileSave or Ctrl+O/Cmd+O in nano, then Ctrl+X/Cmd+X).

Final Step: Flush Your DNS Cache

After saving, your operating system will still hold onto old DNS information for a short time. To ensure your computer immediately uses the new hosts file entry, you must clear the local DNS cache.

Windows

Open Command Prompt and run:

ipconfig /flushdns

macOS

Open Terminal and run:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Linux

The exact command varies by distribution, but try one of the following:

sudo systemctl restart network-manager

or

sudo /etc/init.d/nscd restart

Your computer will now resolve your domain to the new server's IP. You can safely browse and test your site!

Reverting the Change

Important: Once you are satisfied with your testing and have pointed your domain's nameservers to OneStopWP (and confirmed propagation), you must revert this change.

  1. Re-open the hosts file using the admin steps above.
  2. Delete or comment out the lines you added (start the line with a # symbol).
  3. Save the file.
  4. Flush your DNS cache again using the commands above.

Further Reading

For more in-depth information about the hosts file, visit the Wikipedia article on this topic.