Dynamic virthosts

Here is the issue.

We are working on giving our developers a 'sandbox' where they can create new sites under a base directory structure we have setup for them. The problem is the sites change from day to day, based on their site name and its a real issue for us to have to keep modifying the virthosts-sandbox.conf file for each site name change.

Listen 123.456.789.012:80
VirtualHost dev.mysite.com>
   ServerName dev.mysite.com
   DocumentRoot /web/sandbox.domainname.com/dev.mysite.com/htdocs
   ErrorLog /var/web/sandbox.domainname.com/dev.mysite.com_error_log
   CustomLog /var/web/sandbox.domainname.com/dev.mysite.com_access_log common
   ScriptAlias /cgi-bin/ "/web/sandbox.domainname.com/dev.mysite.com/cgi-bin/"
   <Directory /web/sandbox.domainname.com/dev.mysite.com>
      Options Indexes FollowSymLinks Multiviews +Includes
      AllowOverride All

Is it possible to use the Rewrite Engine (or any other module) to just direct the site instead, based on what the DNS entry is seen as? Our DNS points each site to a CNAME like: sandbox.domainname.com

      - dev.mysite.com would point to /web/sandbox.domainname.com/dev.mysite.com
      - dev2.mysite.com would point to /web/sandbox.domainname.com/dev2.mysite.com

And so on.

We just don't want to have to keep modifying the virthosts-sandbox.conf for each new domain name being addressed. We are talking about several hundred sites found in the virthosts-sandbox.conf file, thus the reason for the request.

Apache Version: 2.0.52 (Red Hat)
PHP Version 5.1.2

Loaded Modules: core prefork http_core mod_so mod_access mod_auth mod_auth_anon mod_auth_dbm mod_auth_digest util_ldap mod_auth_ldap mod_include mod_log_config mod_env mod_mime_magic mod_cern_meta mod_expires mod_deflate mod_headers mod_usertrack mod_setenvif mod_mime mod_dav mod_status mod_autoindex mod_asis mod_info mod_dav_fs mod_vhost_alias mod_negotiation mod_dir mod_imap mod_actions mod_speling mod_userdir mod_alias mod_rewrite mod_proxy proxy_ftp proxy_http proxy_connect mod_cache mod_suexec mod_disk_cache mod_file_cache mod_mem_cache mod_cgi mod_php5 mod_ssl

-- Michael
LVL 29
Michael WorshamInfrastructure / Solutions ArchitectAsked:
Who is Participating?
you're using ip based virtual hosts when you should be using name based virtual hosts. there are other ways to keep the url in there but name based virtual hosts are a good idea moving forward for you.  comment out all the rewrite stuff....

change the following directives to.....

NameVirtualHost *
<VirtualHost *:80>

comment out....
Listen 111.222.333.444:80

i believe mod_rewrite can do what you want but i cannot help with creating those expressions.  wouldnt it be much simpler to just append folder names to the end of a url like dev.mydomain.com/devsite1/?

I've messed with this on my development servers a bunch and i've found that the new folder method is the simplest and does what i need it to do.  just a thought.
Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
Actually that is what we are looking to do as seen in the original question, but don't know what we need to do with the mod_rewrite to pass the site to that address. Is it possible to have something like a /web/sandbox.domainname.com/$requested_url/htdocs in the Virthosts * entry or ?

Web Browser URL --> dev.mysite.com

DNS sees the request for dev.mysite.com and resolves it to sandbox.domainname.com

Linux Apache/PHP instance sees a request for dev.mysite.com and points the user to /web/sandbox.domainname.com/dev.mysite.com/htdocs

The type of sites that are being built out are Joomla and Drupal sites.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

funny, we do the same mass production with joomla and drupal.

so are your virtual sites arent working at all?  your hostname never changes right?  so you have http://thisdoesntchange.thisdoesnteaither.com/thischangesallthetime/?

Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
Right now we have the following built out as out virthosts-sandbox.conf:

NameVirtualHost 111.222.333.444
Listen 111.222.333.444:80
<VirtualHost 111.222.333.444>
   ServerName sandbox.domainname.com
   DocumentRoot /web/sandbox.domainname.com
   ErrorLog /var/web/sandbox.domainname.com/error_log
   CustomLog /var/web/sandbox.domainname.com/access_log common
   ScriptAlias /cgi-bin/ "/web/sandbox.domainname.com/"
   <Directory /web/sandbox.domainname.com>
      Options Indexes FollowSymLinks Multiviews +Includes
      AllowOverride All

   RewriteEngine on
   RewriteRule ^(.*)$ http://sandbox.domainname.com/$1 [L,R]

We just don't know where to go from here as we have always built out sites, placing their name in the VirtHosts entry (i.e. <VirtHosts dev.mysite.com>, etc).
Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
I don't think its possible to comment out the Listen portion as we have several other httpd's running on the same box for other departments.

I tried to modify the NameVirtualHost, and I get this error upon '/etc/init.d/http-sandbox configtest':

[Thu Aug 09 08:31:14 2007] [error] VirtualHost _default_:443 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results
Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
Nevermind. I got that part working. Had to modify one other file.

Now I just need the Rewrite Cond and Rules. Currently I have (which is probably not right):

   RewriteEngine on
   RewriteCond %{HTTP_HOST} ^([^\.]+)\.sandbox\.domainname\.com$ [NC]
   RewriteCond %{HTTP_HOST} !^$
   RewriteRule ^(.*)$ http://sandbox.domainname.com/$1/htdocs [R]

I just need the path to resolve to http://sandbox.domainname.com/mysitename.com.

Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
Also posted this message on another site (http://forum.modrewrite.com) and someone posted about using mod_vhost_alias instead to do what I was looking for. After a bit of research, I stumbled upon this site:


-- M
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.