A free and open-source book on ZF3 for beginners

2.11. Hosts File

When you have multiple local websites mapped to different ports, it becomes difficult to remember on which port each site presents. To simplify this, you can use name-based virtual host and define an alias for your website in your system hosts file.

First, modify your Apache virtual host file to be name-based virtual host:

<VirtualHost *:80>
    # Add the ServerName directive
	ServerName site1.localhost	

Next, you should edit the hosts file. The hosts file is a system file which contains mappings between IP addresses and host names. The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names.

To add an alias for your local websites, add lines for each of your website as shown in the example below.            site1.localhost

So now you'll be able to simply enter "site1.localhost" in your browser's address bar instead of remembering the port number.

In Linux, the hosts file is located in /etc/hosts. In Windows, the file is typically located in C:\Windows\System32\drivers\etc\hosts. To edit the file, you need to be an administrator. Please also note that some anti-virus software may block changes to hosts file, so you'll have to temporarily disable your anti-virus to edit the file, and enable it after.

If you have purchased a real domain name for your website (like example.com), you do not need to modify your hosts file, because Apache will be able to resolve the IP address of your website using the DNS system. You modify your hosts file only when DNS system knows nothing about the domain name and can't resolve the IP address of your website.