title: Apache dynamic wildcard host rewrite with dynamic subdomains
body: <blockquote>
    <p>I'm currently in the process of converting an old apache vhost, where <em>one is currently created for each new release</em>, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using <strong>apache v2.2</strong>.</p>
  </blockquote>
  
  <h2>The Goal</h2>
  
  <p>Dynamic all the things. To have a single virtual host that handles all redirects for a specific set of subdomains. The url is below, note that <code>sub1</code> and <code>branch</code> are <em>dynamic</em> and could be anything.</p>
  
  <pre><code>sub1.branch.sandbox.domain.com
  </code></pre>
  
  <p>The directory structure for this is as follows:</p>
  
  <pre><code>/var/www/vhosts/branch/sub1.branch.sandbox.domain.com
  </code></pre>
  
  <p>As you can see above, the directory structure has the <code>branch</code> as a sub-directory before the full url is the name of another sub-directory.</p>
  
  <p>Also, <code>/images/</code> in the url needs to forward to <code>shared-httpdocs/images</code> for each domain.</p>
  
  <h2>The vhost I have so far</h2>
  
  <pre><code>&lt;VirtualHost *:80&gt;
          ServerName branch.sandbox.domain.com
          ServerAlias *.branch.sandbox.domain.com
  
          VirtualDocumentRoot /var/www/vhosts/branch/%0
  
          Options Indexes FollowSymLinks
  
          # Rewrite Engine Stuff Here
          RewriteEngine On
  
          DirectoryIndex index.php
  
          # Assets needs to be forwarded - currently not working
          RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
          RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]
  
          # The HTTP dispatcher - currently not working
          RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
          RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <h2>The old host I'm trying to copy</h2>
  
  <p>This is the old vhost I'm trying to convert from. It's horrible, messy, and our ops <em>has to create a new DNS entry every time</em>. What a joke! I need to sort this out...</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      # Notice how the stupid convention below will require a new DNS entry each time?
      ServerName sandbox.branch.domain.com
      ServerAlias sandbox.api.branch.domain.com
  
      DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs
  
      &lt;Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs"&gt;
              allow from all
              order allow,deny
              # Enables .htaccess files for this site
              #AllowOverride All
  
              RewriteEngine On
  
              # Rewrite all non-static requests to go through the webapp
              RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
              RewriteRule .* - [L]
  
              # Rewrite everything else to go through the webapp
              RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]
  
      &lt;/Directory&gt;
  
      &lt;Directory  "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers"&gt;
          allow from all
      &lt;/Directory&gt;
  
      # Allow us to rewrite to the webapp without it being in the webroot
      Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers
  
      # Get shared/ to point to the shared static resources
      Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs
  
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>A new DNS entry is required each time we have a new branch, so I'm trying to mitigate this by providing a dynamic subdomain vhost (see the vhost I have so far). I've gone from not even being able to match <code>/images/</code> in the url to a permanent redirect loop.</p>
  
  <p>How can I achieve my goal? I know it's a little complex. If I can't do it, I'll just have to write a script that will generate a new vhost each time but a dynamic one that 'just works' would be fantastic. I've put two days into this so far, I'm no sysadmin. Your help would be greatly appreciated.</p>
  
  <p>Resources I have been using:</p>
  
  <ul>
  <li><a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#EnvVar" rel="nofollow noreferrer">mod_rewrite official docs</a> - Shows the basics like things on conditions with <code>REWRITE_COND</code></li>
  <li><a href="https://stackoverflow.com/questions/49500/apache-rewrite-based-on-subdomain">Sub domain rewriting</a> - A question on subdomain rewriting</li>
  <li><a href="https://stackoverflow.com/questions/13790313/mod-rewrite-subdomain-to-folder-break-paths-to-css-and-images">Asset rewriting</a> - Another question on rewriting things like images / css / js, which doesn't seem to work for me</li>
  </ul>