Have you heard about hosts file? Maybe you don’t know what the ‘hosts’ file is, if you don’t, we will teach you how to use it. Today we will show you how to use hosts file to preview a website without switching DNS.
On a cPanel server: How can I preview a website without switching DNS after migration?
On cPanel based servers there are two ways to test and preview a website without changing DNS. Let’s start.
Use Preview URL from cPanel
- Find your server IP
- On your browser type: http://126.96.36.199/~username
Replace 188.8.131.52 with your real server IP address. And replace username with your cPanel/ FTP username.
If you load that URL and it gives you a 404 response, then it seems the temporary URL in cPanel is disabled, you may contact your web hosting provider so they can enable this for you.
The second method, described above, applies to all kind of web hosting control panels, even servers without control panel.
Preview a website by altering your ‘hosts’ file
There is a file in all operating systems called ‘hosts’. It’s a simple plain text file, that is used to map IP addresses to host names on local networks but it’s also useful for internet hosts.
In clear words, hosts file is one of the methods our local PC or Notebook uses to map IP address to host names. Before DNS servers were introduced into the main internet core, the hosts file was really used on almost all local networks.
However, nowadays it’s almost never used, unless you want to force an address to respond from an IP address, for example when you are migrating a website from one server to another.
Only users with administrator privileges (root in linux, or an user with sudo privileges) can edit this file. Hosts file is located on different locations depending on the operating system you are using.
hosts file location on Windows Operating System
- Windows 95/98/Me it’s located at: C:\\Windows\\hosts
- Windows NT/2000 the path is: C:\\WINNT\\System32\\drivers\\etc\\hosts
- Windows XP/2003/Vista/7/8/10 it is located at:
host file location on LInux & Mac OS
- Mac OS users: hosts file can be found at: /private/etc/hosts
- Unix / Linux systems: you can find the hosts file at /etc/hosts
How can I use hosts file to preview a website without switching DNS?
hosts file will always include one or two lines at the beginning, you must NOT modify those lines.
It can also look like this:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
If you are web developer or webmaster and you need to preview a website without switching DNS, you can include your domain name at the hosts file and map it to your server IP address. This will force your domain name to respond to your new server, without chaning DNS at all.
Move to the end of the file, and add this line:
184.108.40.206 nixcp.com www.nixcp.com
Replace “220.127.116.11” with your real IP address, and “nixcp.com” with your real domain name.
You can preview multiple domain names by adding more hosts to the line, example:
18.104.22.168 nixcp.com www.nixcp.com cpanel.com www.cpanel.com
Save the file, open your browser, clear your cache and finally type: www.yoursite.com
Now you are having a test preview a website without switching DNS name servers. It’s specially useful after server migrations, when you need to pre-visualize your website content to make sure everything is working normally.
Once your tests are done, remove the line from the hosts file and change your DNS name servers.
[easy-tweet tweet=”hosts file can help you to preview websites without switching DNS” user=”cpanel_tips” url=”http://nixcp.com/test-preview-a-website-without-switching-dns/”]