2

On my local dev server, I set up test sites with the domain like this projectName.dev

Using the setting below in my Apache config file...

NameVirtualHost *:80

<VirtualHost *:80>
   VirtualDocumentRoot  E:/Server/htdocs/projects/%1/www
   ServerAlias *.dev
</VirtualHost>

This allows me to access projectName.dev in the browser and it will load the files from E:/Server/htdocs/projects/projectName/www/

I would like to modify this so that I can also access the same folders if I use the subdomain www so in my example projectName.dev and www.projectName.dev would load the same files.

Can someone help me to do this? I don't understand the documentation very well

2 Answers 2

2

You could try the following

NameVirtualHost *:80

<VirtualHost *:80>
   VirtualDocumentRoot  E:/Server/htdocs/projects/%-2/www
   ServerName staging.dev
   ServerAlias *.dev www.*.dev
</VirtualHost>

You can use %-2 to always get the "project" name. %-2 give you the second part of the url from the end. For e.g., if url would be http://project1.dev/ then the docroot would be E:/Server/htdocs/projects/project1/www

3
  • Thanks for the help. Unfortunately this results in the same problem I kept running into...the project.dev will load the correct data and www.project.dev loads a default apache 404 page
    – JasonDavis
    Aug 12, 2013 at 16:22
  • I have updated my answer.
    – ALex_hha
    Aug 12, 2013 at 19:15
  • 2-3 years of trying to get this to work for my Dev server...I have asked this exact question at least 2-3 times on Serverfault in that time and you are the first one to give me a working solution...THANK YOU!!!
    – JasonDavis
    Aug 12, 2013 at 19:37
1

The ALex_hha reply is correct, the problem is the %1 in the VirtualDocumentRoot. that is replaced by the hostname used. using both multiple wildcard variants and VirtualDocumentRoot is always tricky.

You have 2 solutions:

  1. replace the %1 my a hardcoded projectName.dev.
    If you have many projects you would need to create a virtualhost config for each project, but just for one is a good workaround.
  2. create 2 configs:

    # www.project.dev format 
    <VirtualHost *:80>
      VirtualDocumentRoot  E:/Server/htdocs/projects/%2+/www
      ServerName www.*.dev
    </VirtualHost>
    
    # project.dev format
    <VirtualHost *:80>
      VirtualDocumentRoot  E:/Server/htdocs/projects/%1/www
      ServerName *.dev
    </VirtualHost>
    

the %2+ says that you want the second and forward part of the hostname received. Check the mod_vhost help page for more info

You are not using a unix, but on unix you could also do a symbolic link from the www names to the project names:

cd /srv/
for i in *; do 
   # bypass if not a directory
   if [ ! -d "$i" ]; then continue; fi
   # bypass if already created
   if [ -e "www.$i" ]; then continue ; fi
   # bypass if already a www
   echo $i | fgrep -q "www" && continue
   ln -s $i www.$i
done

This way both the projectname and www.projectname point to the same place

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .