I know it's possible to setup this system using a code of code, and a lot of pain-in-the-butt to maintain virtual hosts, but is there a dynamic way where it checks to be sure the folders exist?

I'd like to setup a system where on a site the default URL would be www.domain.com and it would look for public_html/live/, but if I set dev.domain.com it would look to public_html/dev/, BUT if I setup dev-live.domain.com it would look for public_html/dev/ and it would use the LIVE database.

I'd like it to be setup so I only have to create a new directory within public_html in order to create a new subdomain, but still be able to maintain the ability to add -[INSERT_DB]


You should be able to use mod_vhost_alias to get the arbitrary virtual hosts working with something like this:

UseCanonicalName Off
VirtualDocumentRoot /home/username/public_html/%1

I'm not sure how you intend on getting Apache to select the database, but you might be able to do that with mod_rewrite and setting DocumentRoot.


Create a new virtualhost for your dev site. Something like:

<VirtualHost *>
        ServerName dev.domain.com
        ServerAlias dev*.domain.com    
        DocumentRoot /my/path/public_html/dev/

That will route both dev.domain.com and dev-live.domain.com to the same place.

Since you also want to switch the databases dynamically, you will also need to change your application code to be smart enough to look at the incoming URL and use the correct database. You probably don't want to do that at the Apache level (just my opinion).

  • With this setup I would still need to have an entry for each repository, is there a way for me to make even this part dynamic, so that whatever I put in "dev" it'll look for that repo, if it's not found, it'll split by - and search for the first part of the split to see if that repo exists? – Webnet Aug 10 '11 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.