To set up a custom domain in localhost we have to apply the change in three places: Setting local development in XAMPP & identifying directory, pointing a custom domain to local computer from Windows host file, and redirecting the domain to a specific application folder through VirtualHost.
Locate Application directory (folder)
Generally, application/website files are located inside htdocs in the main XAMPP folder. For localhost/mylocalapp directory C:/xampp/htdocs/mylocalapp.
Update Windows Host file
Now, Update the Windows host file to tell the local DNS to redirect your domain into your local computer (same Computer).
- Step 1: Go to C:\Windows\System32\drivers\etc and edit hosts file “as Administrator”. [ For that open, any text editor in administrator mode then navigate to hosts file.]
- Step 2: Add 127.0.0.1 and custom domain at the end of file.
127.0.0.1 mylocalapp.test mylocalapp.local
You can add as many domain names as you want by separating them with space. Or can add on each new line like: 127.0.0.1 mylocalapp.local
- Step 3: Save and close.
After getting a request on the same localhost server, let’s tell which application to execute on a particular domain request with a virtual host.
Update Apache virtual host.
Virtual host redirects a particular domain to a specific application file directory in the server.
- Step 1: Go to C:\xampp\apache\conf\extra and open httpd-vhosts.conf file.
- Step 2: Add virtual host configuration for particular URL at the end of file.
<VirtualHost *:80>
ServerName mylocalapp.test // URl to set
ServerAlias mylocalapp.local // Another URl to open same Web App
DocumentRoot C:/xampp/htdocs/mylocalapp // Application file directory (folder) in XAMPP localhost
</VirtualHost>
You can add multiple alias names (domain name) by separating with space or can add another ServerAlias line in the new line.
- Step 3: Save and close.
Now, your custom domain for local applications is ready.