Home > Help Center > General > What is a Hosts file and how to use it properly?

What is a Hosts file and how to use it properly?

The Hosts file is a system file used by the operating system to link host name to an IP address.

In the What is the Domain Name System? article, we pointed out that the browser first asks the operating system for a particular IP address and if the operating system does not have any cached information, it sends a request to resolving name servers.

In addition to the cache (temporary memory), the operating system also checks the Hosts file entries, and if it finds a correct record, it does not send a query to the DNS resolver, but returns the IP address from the Hosts file in response to the browser.

hosts file

Here's a sample host file: localhost loopback :: 1 localhost hostpulse-demo.com www.hostpulse-demo.com

Regardless of the actual IP address of hostpulse-demo.com, all web browsers on computer with the above HOSTS file content will send a direct HTTP request to IP in order to load hostpulse-demo.com website.

Below, we'll show you some examples of how you can use the Hosts file.

Transferring website to another server

When we transfer a website to a new server, it is very important to make sure that all site's functionalities are working correctly from the new server before changing the DNS of the domain.

The Hosts file features described above give us a great opportunity to test how the website works on the new server while at the same time Internet users will continue to load the website from the old server.

All you have to do is to create a record in the Hosts file by specifying the IP address of the new server, clearing the browser's cache and loading the site.

To make sure you are connected to the new server, first make a small change to any title, save the file and refresh the browser - if you see the change, it means you are working on the new server.

If you do not see the change you should find the error somewhere in your actions.

This way, we can be confident that changing the server will not lead to website downtime, nor to loss of customers or visitor's traffic.

Replace Localhost with Domain Name

Quite often, website developers install a web server on their own computer to develop sites instead developing directly on the hosting server (something we also strongly recommend).

One of the most popular apps is XAMPP.

But XAMPP works with URL addresses like: http://localhost/sitedir/

When it's time to upload the website on the server, all addresses must be rewritten by replacing localhost/sitedir with the real domain name.

In addition to the files, the same replacement must be done in the database also (these days almost every new site is built upon Wordpress).

Which means you need to create database archive, to open the .sql file, to execute find/replace operation, to save the file, to delete old database tables and to import the edited archive.

All these actions are not that complicated, but it takes time and some reading is required.

But the development of our website can be much easier with the help of the Hosts file.

Create new entry in the Hosts file as shown below: hostpulse-demo.com

If you are using XAMPP create a virtual host following these steps:

Open file: C:\xampp\apache\conf\extra\httpd-vhosts.conf (If you are using different application follow their specific instructions).

Copy the sample code and create a new virtual host for the actual domain name:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName hostpulse-demo.com
ServerAlias www.hostpulse-demo.com
DocumentRoot "D:/xampp/htdocs/example"
ServerAdmin admin@hostpulse-demo.com

Restart XAMPP and start working as if you are working directly on the web hosting server.

You can install whatever CMS you like, it will use the domain name during installation and you won't have localhost URL in your website files or in the database.

When you choose to move the website to a web hosting server, you only need to create a database, update data base connection settings in config.php file, import the local archive, and upload the files through the FTP, it will take no longer than 2-3 minutes to get these actions done.

Of course, before you test how the website from the hosting environment, you must open the hosts file, comment the appropriate row, save the file, and delete the browser's cache:

# hostpulse-demo.com

Termination of Over Activity

The third way to use the hosts is to stop the excessive activity of some installed programs that for example ask for updates, or it asks to register it as a default program, or is asking me to participate in polls, etc.

When the program opens a web page, I copy the URL and point the domain to IP in the hosts file.

You should experience no more unwanted pop ups.

Where to find the hosts file in your OS?

The hosts file is a plain text file without a file extension and can be edited with any text editor. In some cases, like in Windows 10 for example, you should run the text editor as an Administrator in order to edit the hosts file.

The location of the file in the different operating systems may vary and below we give the sample locations in the most common ones:

Unix, Unix-like, POSIX/etc/hosts
Microsoft Windows%SystemRoot%\System32\drivers\etc\hosts
Apple Macintosh/etc/hosts (a symbolic link to /private/etc/hosts)
Android/etc/hosts (a symbolic link to /system/etc/hosts)

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support