virtual hosting folder structure

David Jones
@david3jones
avatar-davidejones

The majority of my time using apache for developing websites has been without the use of virtual hosts. I have of course encountered this setup in many places but its not something I ever considered I needed to use. The last year or so I had decided that it was time for a better organisation of my sites and so I began to look at this and how it could improve my current setup. I generally like to install apache, php and mysql manually and my basic setup would involve a folder on my hard drive somewhere called www which would store directories and sites that can be accessed by using the full path in the browser e.g http://localhost/mynewsite/ After some thought I decided I wanted to setup something similar by having a www folder but dividing this into directories for the first letter of each website. So for example if a have a site called test I would like it to be located at c:\www\t\test.lc\ using test.lc as a domain. With this i need to setup a domain rather than the full local path to the website files so instead of http://localhost/t/test/ i would have http://test.lc. Using .lc rather than .com or any other extension so that it wouldn’t conflict with the real websites online and the lc standing for localhost. Setting up the virtual hosts I used the following configuration in my apache config files. What this does is when you visit a domain like test.lc it will take the first letter and the full name and attempt to access the site using it in a path like this C:/www/t/test.lc/public_html

<virtualHost 127.0.0.1:80>
       	VirtualDocumentRoot "C:/www/%1.1/%0/public_html"
    	RewriteEngine on
    	RewriteCond %{LA-U:SCRIPT_FILENAME} ^/www/(.*)/(.*)/public_html/[^/]+
    	RewriteRule ^ - [E=VIRTUAL_DOCUMENT_ROOT:/www/%1/%2/public_html/]
</virtualHost>

For this to work i also needed to add the domain test.lc to my hosts file for my local dns so that it will recognise that this domain exists and where it should point to. To do this i opened c:\Windows\System32\drivers\etc\hosts and added in the following

127.0.0.1  test.lc

It all worked perfectly but lastly I wanted a quick way to set this all up so that I can begin a new website project without having to edit multiple files and create folders etc. So I created a simple batch file to do all the dirty work for me. What this does is prompt you for the domain name and then adds it to the hosts file and creates the appropriate folder structure allowing you to get started working on your website straight away.

@ECHO OFF
set /p sitename= Please enter the new website name e.g domain.lc:
ECHO adding to hosts file...
ECHO 127.0.0.1 %sitename%  >> c:\Windows\System32\drivers\etc\hosts
ECHO creating directory structure...
IF NOT EXIST  c:\www\%sitename:~0,1% (
    	mkdir c:\www\%sitename:~0,1%
)
IF NOT EXIST  c:\www\%sitename:~0,1%\%sitename% (
    	mkdir c:\www\%sitename:~0,1%\%sitename%
)
IF NOT EXIST  c:\www\%sitename:~0,1%\%sitename%\public_html (
    	mkdir c:\www\%sitename:~0,1%\%sitename%\public_html
)
ECHO all done!
PAUSE

Comments

    Comments are currently closed