Replace www with a subdomain | XAMPP | Mac | Localhost

After searching the web I found many articles that showed me how to set up my localhost to use subdomains but many missed critical steps. Most say there are 2 steps when sometimes there are 3 steps.
For those of you who don't know what a subdomain is...it is when you replace www at the beginning of your URL with some word like "products", for example mail.google.com where mail is the subdomain. Another example would be for the url www.actionscript-flash-guru.com I could replace the www with products.actionscript-flash-guru.com. Lets use products as our example subdomain.
we assume you have installed XAMPP already and correctly configured. Fair warning: One thing I ran into while configuring XAMPP on my Mac was the continual problem I had with permissions. MySQL wanted read/write access to certain folders and so did the httpd proccess which is the apache web server.
Step 1:
First edit your host file:
which you can find under a few different places depending on what you are using on mac os x lion it is here
/private/etc/hosts
You can use the root user from the command line to edit this file using nano (a very old text editor that comes with Unix which is the operating system on which OS X is based)
sudo nano /private/etc/hosts
This requires that you enable the root user which by default on the mac is disabled. To enable the root user you should search the mac support database. That is where I found out how to do it.
or you can temporarily set the permissions of hosts to include your user assuming your user has admin privileges. Note: for some reason I had to add my user to hosts>ctrl+click>get info>Sharing & Permissions many times before it actually showed up. Then I added read write privileges to my user and closed the get info panel. I could then open it using TextEdit.
I added my new host under "127.0.0.1 localhost"
127.0.0.1 products.localhost
Then I saved it and remove my name from the permissions list. I then restarted the computer, turned on my web server and tested products.localhost if it directs you to your htdocs folder and the web server serves up the web page then step one has been completed.
Step 2:
This is the step I found so often missing in other articles. You have to make sure that the web server is using the virtual hosts file. Right now the IP of your web server is 127.0.0.1 only your computer has access to this IP. You have two different host names resolving to 127.0.0.1 the localhost URL and the products.localhost URL. What you need now is for the server to detect through which URL you are accessing the site and change its behavior based on each URL. So for localhost it will route the requests to htdocs and for the products.localhost we will route the requests to htdocs/products. To do this we will first turn on the virtual hosts file which can be done using the Mac Finder. Go to Applications > xampp > etc and open the httpd.conf file.
Change :

# Virtual hosts
# Include /Applications/xampp/etc/extra/httpd-vhosts.conf

To:

# Virtual hosts

Include /Applications/xampp/etc/extra/httpd-vhosts.conf
Step 3:
Change the virtual hosts file. You can locate it at /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf
save a copy of it so that you can back up to the old if you mess up.
Now cut and paste the following.
Now restart apache and go to the url products.localhost and it should serve the index file from the /Applications/XAMPP/xamppfiles/htdocs/products file.
If any of your files have spaces in the titles you might want to put quotes arount each path. For example:
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/my site"


comments powered by Disqus