Create Sub Domain – Apache Virtual Host on LocalHost

apache-2.4subdomainvirtualhost

My Question in the title may be wrong, but I have no other Idea how to word it. (Experts please help with this?).

Basically sub domains works on my Development Machine when I got to URL http://subdomain.localhost. What I am trying to achieve is loading that same website using http://subdomain.jacques. jacques being the name of my computer.

This is what I have in my hosts file in windows.

127.0.0.1   localhost
localhost   localhost
jacques localhost

subdomain.127.0.0.1 subdomain.localhost
subdomain.localhost subdomain.localhost
subdomain.jacques   subdomain.localhost

This is what I have in the vhosts.conf file.

ServerName localhost
DocumentRoot "c:\web"

<VirtualHost jacques:80 jacques *:80>
    <Directory "c:\web">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
    ServerName localhost
    ServerAlias localhost
    DocumentRoot "c:\web"
</VirtualHost>

<VirtualHost subdomain.jacques *:80>
    ServerName subdomain.localhost
    ServerName subdomain.jacques
    ServerAlias subdomain.localhost
    ServerAlias subdomain.jacques
    DocumentRoot "C:\xampp\htdocs\subdomain"
    DirectoryIndex index.html index.php
    <Directory "C:\xampp\htdocs\subdomain">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

What I have tried from reading the documentation on the Apache website:

  • Adding the PC name to the <VirtualHost> tag.
  • Adding another ServerName/ServerAlias to the Virtual Host
  • Adding entries into the hosts file in Windows

I have no idea what I am doing wrong, so even if you can answer the question, please point me in the right direction.

System Details

  • OS: Windows 10
  • Apache Version: 2.4
  • PHP Version: 5.4 (Not sure if this is useful or even relevant.)
  • Apache Install Path c:\amp\apache
  • PHP Install Path c:\amp\php

Best Answer

This is what I have in my hosts file in windows.

this is wrong format. Must be - "ip address fqdn"

127.0.0.1 localhost jacques subdomain.localhost subdomain.jacques

As for virtual hosts

<VirtualHost *:80>
   ServerName localhost
   DocumentRoot "c:\web"

    <Directory "c:\web">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain.localhost
    ServerAlias subdomain.jacques

    DocumentRoot "C:\xampp\htdocs\subdomain"
    DirectoryIndex index.html index.php

    <Directory "C:\xampp\htdocs\subdomain">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

You can use domain (and any subdomain) lvh.me for testing purpose. As it resolves to 127.0.0.1. So there is no need to edit hosts file

# host lvh.me
lvh.me has address 127.0.0.1
lvh.me mail is handled by 10 mail.lvh.me.

# host subdomain.lvh.me
subdomain.lvh.me has address 127.0.0.1
Related Topic