HavaLite CMS

A new liteweight Content Management System (CMS)

Subdomains on localhost using Apache / XAMPP

http://jaredhocutt.com/programming/environment/subdomains-on-localhost-using-apache-xampp

This post will show you how to create a subdomain similar to http://subdomain.localhost/ on your local install of Apache. For this tutorial, I will be focusing on how this accomplished in the Apache install that is part of XAMPP. These steps should also work for a standalone install of Apache except that the file locations may be different.

I am going to assume that you have already downloaded and installed XAMPP in it’s default location at C:xampp. If you have XAMPP installed in a different location, then change any paths to files accordingly.

The first file that you need to edit is C:xamppapacheconfextrahttpd-vhosts.conf.

You need to make 2 edits to this file. First, uncomment the following directive to tell Apache you are going to use a subdomain:

1
NameVirtualHost *:80

Next, you want to setup your subdomain. Add the following code to the end of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "C:/xampp/htdocs"
 
    DirectoryIndex index.php
 
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
 
<VirtualHost *:80>
    ServerName subdomain.localhost
    DocumentRoot "C:/Users/Jared/Documents/web/subdomain"
 
    DirectoryIndex index.html index.php
 
    <Directory "C:/Users/Jared/Documents/web/subdomain">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

The first VirtualHost will allow you to still put files in the normal htdocs folder and serve those using Apache. The second VirtualHost is the one for your subdomain. The things that you need to update are the following:

  • ServerName: this should be the subdomain you want to setup.
  • DocumentRoot: this should be where the files that should be served by Apache are located
  • Directory: this should match the DocumentRoot

The second file that you need to edit is C:WindowsSystem32driversetchosts.

Add the following to the end of the file to tell Windows that when you navigate to subdomain.localhost that is should look for it locally instead of trying to resolve the domain name. Note that this should match the ServerName from the Apache configuration.

1
127.0.0.1       subdomain.localhost

That’s all there is to it. Restart Apache and your subdomain should be working. If you want to add multiple subdomains, repeat the steps in this article, minus the VirtualHost for localhost as that is only needed once.

If you have any trouble getting this setup or suggestions, please leave them in the comments.

comments powered by Disqus

0 Comments

    Leave a Reply

    Contact Info

    Phone: +49 941 26175

    Address: Ayman Teryaki,
    Prüfeninger Str. 48,
    93047 Regensburg, Germany

    Want more info - go to our contact page or visit Google+

    Social

    Stay up to date. Subscribe via RSS, Facebook, Twitter or Email

    Recent Comments