Dynamic Virtualhost on Wildcard Subdomain hidden behind CNAME

apache-2.2cname-recordvirtualhost

Introduction

I have a community and every user can has its own subdomain at my project e.g. username1.mydomain.com

In PHP I use the variable HTTP_HOST to determinate the username (username1).

Task

Let the user route it's own domain to his subdomain by CNAME record.
www.usersdomain.com should link to username1.mydomain.com.

Problem

After adding the CNAME Record (www.usersdomain.com) the HTTP_HOST variable should return
username1.mydomain.com instead of www.usersdomain.com.

How can I fix this issue?
Hope somebody can help me, please.

Current Virtual Host Setting

    <VirtualHost *:80>
    DocumentRoot /home/webapps/egoapp/current/public
          <DirectoryMatch  /\.git/|/\.svn/ >
           Deny from all
         </DirectoryMatch>
         <Directory "/home/webapps/egoapp/current">
          Options FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
         </Directory>

     RewriteEngine On

     # Enable status page for monitoring purposes                               
     RewriteCond %{REMOTE_ADDR} ^(127.0.0.1)                                    
     RewriteRule ^(/server-status) $1 [H=server-status,L]                       

     # Redirects to a maintenance page if the specified file below exists       
     # ...but it still allows images to be served                               
     RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f                    
     RewriteCond %{SCRIPT_FILENAME} !/system/maintenance.html                   
     RewriteCond %{SCRIPT_FILENAME} !^(.+).(gif|png|jpg|css|js|swf)$            
     RewriteRule ^.*$ /system/maintenance.html [L]           

     # <CUSTOM RULES BEFORE FORWARDING END>
     # Proxy the rest to the load balancer
     RewriteRule ^/(.*)$ http://127.0.0.1:85%{REQUEST_URI} [P,QSA,L]

     # Setup the logs in the appropriate directory
     CustomLog /var/log/httpd/access_log combined
     #ErrorLog  /var/log/httpd/error_log

     #Remote logging -- handle by syslog
     ErrorLog "|logger -p local3.info -t httperror"
     CustomLog "|logger -p local3.info -t http" combined

     LogLevel warn

     # Deflate
     AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+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
     SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
</VirtualHost>

Ideas for solving this problem

I think I have to edit the /etc/resolv.conf, but I don't know how 😉
After Playing a little bit I can change the Servername to 127.0.0.1 or "undefinded domain"
but not to username1.mydomain.com

Current Resov.conf

search eu-west-1.compute.internal
nameserver xxx.xxx.xxx.xxx

Best Answer

The HTTP_HOST variable is created from the actual URI request that the client sent to your virtual host. This actually has nothing to do with your DNS, it's just a string, so editing your resolv.conf will not help. At the point that the client is connecting to the server and providing that HTTP_HOST, it has already done the work of resolving DNS and determining which IP address it wants to send the request to for that URI.

I think your easiest solution would be to store the association between www.usersdomain.com and username1 in a database of some kind and compre the HTTP_HOST variable to the database to determine which user's site to return.

Your only other option would be to manually configure new virtualhosts for each of these domains. This can be time consuming and error prone, I would go with a database driven method that could be integrated into your site and would be relatively automated.

Related Topic