- 1 - THE NEED FOR VirtualHost
- 2 - How to create a VirtualHost

- 1 - THE NEED FOR VIRTUALHOST
The use of VirtualHost is mandatory with Apache to correctly use WEB applications, CMS or your local sites. It is essential to create a VirtualHost for each of your projects, even if they are in the \wamp64\www\subfolder structure.

Launching local sites via 'http://localhost/project1/' or 'http://localhost/project2/' is ABSOLUTELY NOT a good solution, and should be avoided altogether.
In effect, you're introducing an extra "localhost/" level into the access urls to your local sites, which means that many variables defined by the HTTP server, such as elements of the $_SERVER['xxx'] array, will not have the right values.
For example, for a wamp64/www/my-project/ project
With the call as it should be: http://my-project/ here are the values of some predefined elements
$_SERVER['HTTP_HOST'] = my-project
$_SERVER['SERVER_NAME'] = my-project
$_SERVER['DOCUMENT_ROOT'] =C:/wamp64/www/my-project
And these are the same elements predefined with http://localhost/my-project/
$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] =C:/wamp64/www
You can see, among other things, that the path to the project's root folder ($_SERVER['DOCUMENT_ROOT']) is the wrong one. And, what's more, it will ALWAYS be C:/wamp64/www, whatever project is launched in this way.
These erroneous values will - for sure - induce incomprehensible errors with Frameworks, CMS or web applications you add to your projects, such as Wordpress or Joomla; but they're not the only ones.
Your "projects" are hosted at "localhost", just as "real" sites are hosted at "ovh", "free", "1and1", "gandi" or others, and to access these real sites, you don't type http://gandi/wampserver.com but http://wampserver.com.
What's more, the folder for a VirtualHost can be anywhere, for example in g:\www\ whereas Wampserver is in c:\wamp64\, so the folder won't be deleted even after Wampserver has been uninstalled.

- 2 - HOW TO CREATE A VIRTUALHOST
Always create a VirtualHost BEFORE installing a Web application or CMS (Joomla, Wordpress, Prestashop, Moodle, etc.).
While this doesn't prevent you from reading everything that follows (and it's even recommended), with Wampserver, creating or adding a VirtualHost can be summed up in a few clicks. It is essential to use Wampserver's internal tools to create or add a VirtualHost.
For ease of explanation, here are a few names:
- sitefolder: folder in which to host the local site
- sitename: name under which the local site will be called http://sitename/
First of all, let's choose the place where we want to host our local site. This can be a folder located in Wampserver, such as wamp64/www/dossierdusite/, but it's not mandatory; this hosting can be anywhere on the hard disk, avoiding paths containing spaces; for simplicity of explanation we choose :
c:\sitefolder\
The folder in which the site will be hosted must exist before the VirtualHost is created, so you need to create it using File Explorer, running as administrator.
- Launch Wampserver, wait for green W icon
- Wampmanager icon -> localhost
This takes you to the Wampserver home page.
- Click on Add a Virtual Host, in the Tools menu at bottom left.
You're now on the Add a Virtual Host page
You're all set to add your first VirtualHost
- In the form
-- For "Name of the Virtual Host", type :
sitename
-- For "Complete absolute path of the VirtualHost folder ", enter :
c:\sitefolder\
-- If you want to use PHP in FCGI mode
Check to use a PHP version other than the default. Choose the PHP version you want from the drop-down menu.
There are two other possible options:
-- If you want to use a "Listen port" other than the default one.
-- If you want to use VirtualHost by IP.
Then validate the form with the button:
-- Start the creation/modification of the VirtualHost
As indicated: this may take some time.
After this "certain time" it is indicated that unfortunately certain operations necessary to the taking into account by Windows and Apache of this new VirtualHost cannot be carried out automatically by the navigator, thus, as indicated:
- Right-click Wampmanager icon -> Tools-> Restart DNS
Wait for the icon to turn green again.
Click on Back to homepage
In the Your VirtualHost menu, on the right, your own VirtualHost is present:
- localhost
- sitename
In the same way, you also have this Vos VirtualHosts submenu with the Wampmanager icon.
Now you're ready to install your local site in the c:\sitefolder\ folder, indicating, if requested during installation, that the url is http://sitename/.

In fact, it should work in exactly the same way as any Apache server on a hosting provider's site, with a few minor differences in terms of access authorizations.
To fully understand the VirtualHost mechanism, it's essential to read the following.
This is the file called `httpd-vhosts.conf` which contains the VirtualHost for Apache
\wamp64\binapacheapache2.4.58\conf\extra\httpd-vhosts.conf
Here's a definition of a VirtualHost in Wampserver :
<VirtualHost *:80>
  ServerName nom-de-mon-site
  DocumentRoot "chemin-du-dossier"
  <Directory "chemin-du-dossier/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride all
    Require local
 </Directory>
</VirtualHost>
Each VirtualHost has its own definitions of
⦁ ServerName which indicates the name of the local site 
⦁ DocumentRoot which indicates which folder it is in.
⦁ Directory which gives the possible options and access authorizations.

-- How VirtualHost works in Apache --
The first definition in this file will also be the default site, so that if the domain name used in the browser doesn't match any defined domain, localhost, the first domain in the file, will be served.
So, we make sure that Apache security for the domain localhost ** ALWAYS HAS THE VALUE**.
Require local
and that it ALWAYS REMAINS FIRST on the VirtualHost list. Any casual hacker from an external address will receive an error and won't be able to enter the PC, but if you misspell a domain you'll see the WampServer home page (or an external domain), because you're on the same PC as WampServer and therefore in `local`.
To add a VirtualHost, you need to add your new domain name to the HOSTS file.
C:\windows\system32\drivers\etc\hosts
The file has no extension and must remain extensionless. Beware of Notepad, as it may try to add a `.txt` extension if you don't have a better editor.  I suggest you download Notepad++, it's free and a very good editor.
It's also a protected file that you'll need to edit with administrator privileges, so you'll need to launch the editor with the "Run as administrator" option.
After editing and adding the project1 domain, the contents of the hosts file should look like this:
127.0.0.1 localhost
::1 localhost
127.0.0.1 project1
::1 project1
Now we need to tell Windows to refresh the domain name cache (otherwise, we'll have to reboot).
Open a command window as administrator and type the command below: 
ipconfig /flushdns
This forces Windows to clear the domain name cache and then reload it. On reloading, Windows will reread the HOSTS file and the new `project1` domain will be known.
To do this, right-click -> Tools -> DNS Restart.

NOTE: Domain name. It's a very bad idea to define a ServerName1 or ServerAlias that is the same as your real domain name. In fact, when you request a url in the browser, the local DNS (hosts file, then local cache) is read first, and if a match is found, the local site will be loaded, making it impossible to access the real site.
A good practice is to give the same name but without the tld, so, for example, mysite.net on the real site and mysite on the local site.
It's also possible to make a few changes to the PHP configuration, which will ONLY apply to this local site. This can be very useful for a site with specific requirements, as opposed to all the other sites you manage.

1 ServerName is a domain name, even locally, and must comply with certain rules. A domain name must be between 2 and 63 characters long. Allowed characters are letters, numbers and the "-" sign. The "-" sign is allowed anywhere except in the first or last position. In ALL CASES, underscores (_) are not allowed in domain names. For more information (but it's pretty boring), see Internationalized Domain Names in Applications, RFC 5890 and also RFC 1034.