How to Create Subdomain in XAMPP

- Sunday, December 14, 2025

Creating a subdomain in XAMPP involves configuring two main files: your XAMPP installation's Apache HTTP Server configuration and your operating system's hosts file.

Here is a step-by-step guide on how to set up a subdomain like sub.localhost for your project:

How to Create Subdomain in XAMPP

1. Configure Apache's Virtual Hosts

The Virtual Host configuration tells Apache how to handle requests for your new subdomain.

  • Locate the httpd-vhosts.conf file: Navigate to the following path in your XAMPP installation directory:

    C:\xampp\apache\conf\extra\httpd-vhosts.conf
    (The path might be different if XAMPP is installed elsewhere, but the relative path \apache\conf\extra\ remains the same).

  • Open and Edit the file: Open httpd-vhosts.conf using a text editor (like Notepad++, VS Code, or Sublime Text).
  • Add the new Virtual Host entry: Append the following block of code to the end of the file. Make sure to replace your_project_name with the actual folder name of your project within the htdocs directory.
    *Note: I recommend keeping your main localhost configuration intact for general use*.
    <VirtualHost *:80>
    	DocumentRoot "C:/xampp/htdocs/your_project_name"
    	ServerName sub.localhost
    	<Directory "C:/xampp/htdocs/your_project_name">
    		Order allow,deny
    		Allow from all
    		Require all granted
    	</Directory>
    </VirtualHost>
    
  • DocumentRoot: This is the absolute path to the folder that contains your project files. This will be the root directory for your sub.localhost domain.
  • ServerName: This is the actual name of your desired subdomain (e.g., blog.localhost, dev.localhost, sub.localhost).

2. Edit the Hosts File (DNS Simulation)

The hosts file is what your computer uses to map domain names to IP addresses. Editing it will tell your operating system that sub.localhost should point back to your local machine (127.0.0.1).

  • Locate the hosts file: The location varies by OS:
    • Windows: C:\Windows\System32\drivers\etc\hosts
    • macOS/Linux: /etc/hosts
  • Open and Edit the file with Administrator/Root privileges: This is crucial! You must open your text editor as an administrator (right-click -> "Run as administrator" on Windows) before opening the hosts file, or you won't be able to save your changes.
  • Add the mapping: Add the following line to the end of the file:

    127.0.0.1   sub.localhost

  • 127.0.0.1 is the loopback address, meaning "this computer".
  • sub.localhost is the subdomain name you defined in the Virtual Host configuration.

3. Restart Apache

For the changes to take effect, you must restart your Apache server in the XAMPP Control Panel.

  1. Open the XAMPP Control Panel.
  2. Click the Stop button next to the Apache module.
  3. Click the Start button to restart it.

4. Test the Subdomain

  1. Open your web browser and navigate to: http://sub.localhost/.
  2. If everything is configured correctly, you should now see the content of your project folder (C:/xampp/htdocs/your_project_name).

Get latest updates in your inbox

Copyright © Orion Technology News 2026. All Rights Reserved.