title: Multiple domains, multiple vhosts, separate rails apps
body: <p>I am trying to host multiple different rails apps on the same VPS using multiple domains.  I am using Apache 2.2.17 on Ubuntu 10.10.  For Apache, I have multiple vhost files so that I can enable and disable particular sites easily without needing to comment them out or delete them.  In addition, I am also using mod_rewrite so that the multiple domains that go to the same rails app appear to go to the same URL so I don't take a hit with search engines as far as duplicate content.</p>
  
  <p>I believe that my DNS is setup properly.  For each of the domains, I have a <code>www</code> subdomain as well as some site-specific sub-domains, such as blogs, etc.  The issue I am seeing is that Apache appears to matching the <code>www</code> subdomain immediately and not examining the additional URL behind it.  Changing <code>ServerAlias</code> does nothing.  For example, using my setup, if I enter <code>davidheartsrachel.com</code>, I properly reach my wedding website.  However, if I use <code>www.davidheartsrachel.com</code>, I reach my other website, my software development business.  The URL is not re-written; it stays as <code>davidheartsrachel.com</code> instead of <code>afewguyscoding.com</code>.</p>
  
  <p>The only way I have been able to properly get it to work is to use mod_rewrite in the primary vhost file to redirect to the wedding website vhost file (you can see that I have that in the primary vhost file, but it is commented out for purposes of this question).  This doesn't seem proper to me?  Should I get another IP and do IP vhosts instead of name-based vhosts?</p>
  
  <p>When I execute <code>apachectl -S</code>, I get the following:</p>
  
  <pre><code>VirtualHost configuration:
  wildcard NameVirtualHosts and _default_ servers:
  *:80                   is a NameVirtualHost
           default server afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
           port 80 namevhost afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
           port 80 namevhost blog.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:69)
           port 80 namevhost lbbs.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:84)
           port 80 namevhost davidheartsrachel.com (/etc/apache2/sites-enabled/davidheartsrachel.com:1)
           port 80 namevhost dhr.afewguyscoding.com (/etc/apache2/sites-enabled/davidheartsrachel_staging:1)
  Syntax OK
  </code></pre>
  
  <p>I notice that <code>afewguyscoding.com</code> is the default site - however, doesn't it have to do a full string match to determine the proper site?</p>
  
  <p><strong>Primary site's vhost file</strong></p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerAdmin david.stites@afewguyscoding.com
    ServerName afewguyscoding.com
    ServerAlias davidstites.com, 5280software.com, milehigh-software.com, milehighsoftware.org
    ServerAlias www.5280software.com, www.milehigh-software.com, www.milehighsoftware.org, www.davidstites.com, www.afewguyscoding.com
  
    # this tells rails that it will run in production mode
    # this is for rails &lt; 3.x
    RailsEnv production
  
    DocumentRoot /var/www/afewguyscoding/current/public
    DirectoryIndex index.html
  
    # custom log file locations
    # possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel alert
    ErrorLog  /var/www/afewguyscoding/current/log/error.log
    CustomLog /var/www/afewguyscoding/current/log/access.log combined
  
    # allows compression of text based mime.types
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  
    FileETag None
    RewriteEngine On
  
    # check for maintenance file and redirect all requests
    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
    RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
    RewriteCond %{REQUEST_URI} !^/ws/
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /system/maintenance.html
  
    #RewriteCond %{HTTP_HOST} ^www.davidheartsrachel.com$
    #RewriteRule ^(.*)$ http://davidheartsrachel.com$1 [L]
  
    RewriteCond %{HTTP_HOST} ^www.davidstites.com$
    RewriteRule ^(.*)$ http://www.afewguyscoding.com$1 [R=301,L]
  
    &lt;Directory /var/www/afewguyscoding/current/public&gt;
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
    &lt;/Directory&gt;
  
    # In case I ever need CGI
    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    #&lt;Directory "/usr/lib/cgi-bin"&gt;
    #     AllowOverride None
    #     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #     Order allow,deny
    #     Allow from all
    #&lt;/Directory&gt;
  
    # how we can restrict access to documents from the local subnet
    #Order deny,allow
    #Deny from all
    #Allow from 127.0.0.0/255.0.0.0 ::1/128
  
    &lt;Location /blog&gt;
      PassengerEnabled off
    &lt;/Location&gt;
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:80&gt;
    ServerAdmin info@afewguyscoding.com
    ServerName blog.afewguyscoding.com
  
    DocumentRoot /var/www/wpress
    DirectoryIndex index.php
  
    &lt;Directory /var/www/afewguyscoding/current/public&gt;
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
    &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p><strong>Wedding site's vhost file</strong></p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerAdmin info@davidheartsrachel.com
    ServerName davidheartsrachel.com
    ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com
    ServerAlias www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
  
    # this tells rails that it will run in production mode
    # this is for rails &lt; 3.x
    RailsEnv production
    # this is for rails &gt;= 3.x
    RackEnv production
  
    DocumentRoot /var/www/davidheartsrachel/current/public
    DirectoryIndex index.html
  
    # Custom log file locations
    # Possible values include: debug, info, notice, warn, error, crit, alert and emerg,
    LogLevel alert
    ErrorLog  /var/www/davidheartsrachel/current/log/error.log
    CustomLog /var/www/davidheartsrachel/current/log/access.log combined
  
    # Allows compression of text based mime types
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  
    FileETag None
    RewriteEngine On
  
    # Check for maintenance file and redirect all requests
    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
    RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /system/maintenance.html [L]
  
    RewriteCond %{HTTP_HOST} ^www.rachelanddavidwedding.com$
    RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
  
    RewriteCond %{HTTP_HOST} ^www.rachelanddavidstites.com$
    RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
  
    # Static cache
    RewriteCond %{REQUEST_METHOD} !^POST$
    RewriteCond /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html -f
    RewriteRule ^(.*)$ /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html [L]
  
    &lt;Directory /var/www/davidheartsrachel/current/public&gt;
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      Allow from All
    &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>If you can think of any more information that might be helpful, please, ask me to post it.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Let me try to clear this up. In the most essential form, my question is: why does going to davidheartsrachel.com takes you to www.afewguyscoding.com and davidheartsrachel.com takes you to davidheartsrachel.com based on my configuration above</p>