How can I preview or modify my website without changing the live site?

If you are in the middle of rebuilding or migrating a website you may want to preview your changes without affecting the live version. This article covers two effective methods:

  1. If your main site is currently hosted elsewhere, the best option is using the Hosts file.
  2. If your live site is on the same server as the edited version, the best option is using a subdomain.

1. Using the Hosts File

If your main site is currently hosted elsewhere while you create a new version on a MyHost server, this is the best method for viewing the new site.

By editing your local machine's Hosts file, you tell your computer that your website's domain should point to a particular IP address. Only your computer (or local machine) is affected. This means that you can tell your computer to display the new version of the website when you visit your domain.

Think of this as your own personal redirect. There are no changes to your live website, or to the way other visitors see it.

How to edit the Hosts file and preview your new website

Windows

  1. Find your server’s IP address (look in cPanel under General Information).
  2. Open Notepad as an Administrator (right-click the Notepad icon and choose Run as Administrator).
  3. In Notepad, click File > Open and navigate to:
    C:\Windows\System32\drivers\etc\hosts
  4. At the bottom of the file, add the following line, replacing 123.456.789.012 with your server's IP and example.com with your domain:
    123.456.789.012 example.com www.example.com
  5. Save the file (ensure it's saved as hosts, not hosts.txt).
  6. Clear your browser’s cache or restart your browser, and then navigate to your domain to preview the version of the site that's on your new server.

MacOS

  1. Find your server’s IP address (look in cPanel under General Information).
  2. Open the Terminal application. You can find it in Applications > Utilities.
  3. Type the following command to edit the hosts file:
    sudo nano /etc/hosts
  4. When prompted, enter your password.
  5. In the Hosts file, scroll to the bottom and add the following line, replacing 123.456.789.012 with your server’s IP and example.com with your domain:
    123.456.789.012 example.com www.example.com
  6. Press CTRL + O to save the file, then press Enter to confirm the filename.
  7. Press CTRL + X to exit the editor.
  8. Clear your browser’s cache or restart your browser, then navigate to example.com to preview your site on the new server.

Linux

  1. Find your server’s IP address (look in cPanel under General Information).
  2. Open the Terminal application.
  3. Type the following command to open the hosts file with superuser privileges:
    sudo nano /etc/hosts
  4. Enter your user password to proceed.
  5. Scroll to the bottom of the hosts file and add the following line, replacing 123.456.789.012 with your server’s IP and example.com with your domain:
    123.456.789.012 example.com www.example.com
  6. Press CTRL + O to save the file, and press Enter to confirm.
  7. Press CTRL + X to exit.
  8. Clear your browser’s cache or restart your browser, then navigate to example.com to preview your site on the new server.

If your browser blocks you for security reasons

If your site (like most modern content management systems) requires HTTPS you may need to generate a self-signed SSL certificate for your server. This will allow you to access the site securely without being blocked by any browser warnings. You can generate a self-signed certificate using cPanel's SSL/TLS section. If you see a warning about the certificate being untrusted, this is normal for self-signed certificates and can be ignored.

Restoring your Hosts file to normal

After you have finished previewing your site, or after it has gone live, you will want to remove the line that you added to your Hosts file. You can either:
  1. Completely delete the line, or
  2. Comment out the line, which leaves it in place but tells the system to ignore it. This is handy if you want to be able to quickly reinstate it later. To comment the line out, add a # at the beginning:
    # 123.456.789.012 example.com www.example.com

Why use the Hosts file?

The Hosts file method is the most reliable for testing changes and migrations. It allows you to force your local machine to resolve your domain to the new server without impacting the live site for other users. This method works across all operating systems and ensures you can test everything in a real-world environment without changing anything about the website's URL.

2. Using a Subdomain (if your live site and edited site are on the same server)

If you prefer not to modify the Hosts file or your main website is on the same server, another method is to create a subdomain, such as staging.example.com, and point it to the server. This allows you to access the new site while keeping the main domain live and unchanged.

Steps to create a subdomain in cPanel

  1. Log into your cPanel account.
  2. Navigate to Domains > Create a new Domain.
  3. Enter the name of your subdomain, such as staging.example.com.
  4. cPanel will automatically set up the subdomain’s folder structure.
  5. Go to the Zone Editor in cPanel and check that an A record for the subdomain has been created and is pointing server's IP address. Note: if your nameservers are pointing to a different hosting provider, this will have to be done there.
  6. Once the DNS propagates, access your website by visiting https://staging.example.com.

Why use a subdomain?

Using a subdomain is a convenient method if you want to preview your site from multiple devices, or if you’re working on updating a site on the same server.

If links don't behave as expected

Be careful of any hardcoded URLs in your site. If you direct a link to /about, it will stay within the subdomain. But if it is coded to point to www.example.com/about, it will open your current live site.

Extra feature for WordPress users

If you are working with WordPress and using cPanel's WP-Toolkit, it has a feature to rewrite the URL across the site. This means that you can apply your live site's URL to your new site, rather than migrate your changes across.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

What database settings do I use for scripts?

Server Hostname: localhost Database Username: <your username>_<database name> e.g....

How do I add a wildcard subdomain for Wordpress MU?

This can be easily done by logging into your cPanel control panel and clicking on "Subdomains"....

Do you support Silverstripe Hosting?

Yes, we run Linux servers designed to run the New Zealand developed Silverstripe application. If...

Strict Standards Errors for PHP

If you are getting errors similar to:Strict Standards: Non-static method JLoader::import() should...

Fix permissions of all directories and files

To change all the directories to 755 (-rwxr-xr-x): find /home/xxx/public_html -type d -exec...