19

I've trying to create subdomain in my local xampp installation for some time. I tried editing my httpd.conf file and I entered the following:

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /ecommerce
ServerName ecomm.localhost
</VirtualHost>

I also edited my windows hosts file and entered: 127.0.0.1 ecomm.localhost

But when I type 'ecomm.localhost' in my firefox it gives me: Access forbidden!!! Can please anybody help me out? What exactly I'm doing wrong? I'm fairly new to this. I simply want to create multiple folders in my 'htdocs' folder and use them as different websites with subdomain. For example: c:\xampp\htdocs\mainSite -----> mainSite.com or mainSite.localhost c:\xampp\htdocs\subSite -----> subSite.mainSite.com or subSite.mainSite.localhost

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Xk0nSid
  • 961
  • 2
  • 11
  • 19

6 Answers6

24

Try this :

NameVirtualHost 127.0.0.1:80
<VirtualHost *:80>
<Directory "C:\path\to\ecommerce">
    Options FollowSymLinks Indexes
    AllowOverride All
    Order deny,allow
    allow from All
</Directory>
ServerName ecomm.localhost
ServerAlias www.ecomm.localhost
DocumentRoot "C:\path\to\ecommerce"
</VirtualHost>

Yes you edited your hosts file correctly.

Jigar
  • 3,256
  • 1
  • 30
  • 51
  • Thanks for the quick reply Jigar. I tried editing my httpd.conf with the above code but it still did not work. Firefox says:"Access forbidden" for ecomm.localhost and "could not find" for www.ecomm.localhost. I'll try the Ampps and see if it works. Thanks for the help. – Xk0nSid Jan 01 '13 at 22:59
  • Sorry I just notices you are using Windows. So replace `/ecommerce` to `"C:\path\to\ecommerce"` – Jigar Jan 02 '13 at 07:55
  • Hey Jigar Thanks. It worked. I installed Ampps as you said and the first thing I did in the control panel was to set password. After that when I pointed my firefox to 'localhost/ampps' nothing happens. The address bar says "http://localhost/ampps/index.php?act=login" but nothing is displayed. Can you help please? I am able to see websites in 'www' folder but not able to see admin control panel. – Xk0nSid Jan 02 '13 at 11:04
  • try this http://www.ampps.com/wiki/Category:FAQs#Accessing_Admin.2FEnduser_Panel_gives_a_Blank_Page open a support ticket here if you are facing any issue http://www.ampps.com/support – Jigar Jan 02 '13 at 11:43
  • Thanks Jigar I was able to resolve the problem by reinstalling Ampps. Thanks again for your support. – Xk0nSid Jan 02 '13 at 13:29
  • Ampps = USELESS for setting up sub domains – Jacques Koekemoer May 22 '15 at 13:11
  • Is there any way to create subdomain in window + XAMPP without restarting Apache services ? – Chintan Gor Jan 05 '18 at 14:51
  • 1
    @ChintanGor you can look at graceful restart `apachectl -k graceful` (https://httpd.apache.org/docs/2.4/stopping.html#graceful) – Jigar Jan 07 '18 at 05:38
  • @Jigar Do i also have to edit `hosts` file in `C:\WINDOWS\system32\drivers\etc` – Rajeev Ranjan Sharma Dec 26 '21 at 10:27
  • Yes @RajeevRanjanSharma, for domain to resolve to an IP we need an entry in `hosts` file. e.g. `127.0.0.1 ecomm.localhost` – Jigar Dec 27 '21 at 12:04
12

In addition to atabak's answer:

Go to Apache > Conf > Extra -> "httpd-vhosts.conf" file and add:

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/subdomain"
ServerName subdomain.localhost.com
</VirtualHost>

Go to C:\WINDOWS\system32\drivers\etc -> "hosts" file and add:

127.0.0.1 subdomain.localhost

from Setting Up Multiple Subdomains Using Xampp /

clearlight
  • 12,255
  • 11
  • 57
  • 75
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
4

In xampp\apache\conf\extra\httpd-vhosts.conf file add these line at the bottom of the file for subdomain support :

<VirtualHost *:80>
   DocumentRoot "C:/xampp/htdocs/sandbox"
   ServerName sandbox.localhost.com
</VirtualHost> 

Then in C:\windows\System32\drivers\etc\hosts file add these line at the bottom of the file :

127.0.0.1    sandbox.localhost.com

After that re-start the xampp server and open a new tab, write in the address bar

sandbox.localhost.com

Then you will see the output of index.php file which was in the sandbox folder

abdtpbd
  • 51
  • 3
3

This worked for me. Paste at the bottom of the httpd-vhost.conf file at xampp > Apache > Conf > Extra. Make sure not to comment any vitualhost tag you're adding or you get "Attempting to start Apache" error when you restart server.. foodporch is the name of my subdomain

<VirtualHost *:80>
    DocumentRoot "c:/xampp/htdocs"
    ServerName localhost
    <Directory  "c:/xampp/htdocs">
       Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/xampp/htdocs/foodporch"
    ServerName foodporch.localhost
    <Directory  "c:/xampp/htdocs/foodporch">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Remember to add this line to end of host file at C:\WINDOWS\system32\drivers\etc -> 127.0.0.1 foodporch.localhost.com

Sambasten
  • 249
  • 3
  • 6
2

in httpd.xampp.conf file add this line for subdomain support :

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/subdomain"
ServerName subdomain.localhost.com
</VirtualHost>

then add : windows hosts file and entered: 127.0.0.1 subdomain.localhost

work for me

atabak
  • 193
  • 1
  • 4
0

To improve on this answer for windows folks. To enable actual saving of hosts file in windows: C:\WINDOWS\system32\drivers\etc -> 127.0.0.1 subdomain.localhost.com you must first open notepad as administrator or else windows wont save the hosts file because system files require administrative permission.

So first on start menu, find notepad, Right click on the icon. Choose Run as administrator. Then open the hosts file. This will allow you to update the hosts file without adding any extensions.

mello
  • 21
  • 2
  • 5