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:
- If your main site is currently hosted elsewhere, the best option is using the Hosts file.
- 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
- Find your server’s IP address (look in cPanel under General Information).
- Open
Notepad
as an Administrator (right-click the Notepad icon and choose Run as Administrator). - In Notepad, click File > Open and navigate to:
C:\Windows\System32\drivers\etc\hosts
- At the bottom of the file, add the following line, replacing
123.456.789.012
with your server's IP andexample.com
with your domain:123.456.789.012 example.com www.example.com
- Save the file (ensure it's saved as
hosts
, nothosts.txt
). - 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
- Find your server’s IP address (look in cPanel under General Information).
- Open the Terminal application. You can find it in Applications > Utilities.
- Type the following command to edit the hosts file:
sudo nano /etc/hosts
- When prompted, enter your password.
- In the Hosts file, scroll to the bottom and add the following line, replacing
123.456.789.012
with your server’s IP andexample.com
with your domain:123.456.789.012 example.com www.example.com
- Press
CTRL + O
to save the file, then pressEnter
to confirm the filename. - Press
CTRL + X
to exit the editor. - Clear your browser’s cache or restart your browser, then navigate to
example.com
to preview your site on the new server.
Linux
- Find your server’s IP address (look in cPanel under General Information).
- Open the Terminal application.
- Type the following command to open the hosts file with superuser privileges:
sudo nano /etc/hosts
- Enter your user password to proceed.
- Scroll to the bottom of the hosts file and add the following line, replacing
123.456.789.012
with your server’s IP andexample.com
with your domain:123.456.789.012 example.com www.example.com
- Press
CTRL + O
to save the file, and pressEnter
to confirm. - Press
CTRL + X
to exit. - 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
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
- Completely delete the line, or
- 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
- Log into your cPanel account.
- Navigate to Domains > Create a new Domain.
- Enter the name of your subdomain, such as
staging.example.com
. - cPanel will automatically set up the subdomain’s folder structure.
- 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.
- 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.