Apache subdomain mapping

From Wiki
Jump to: navigation, search

Apache can map subdomains using the mod_vhost_alias module. This module can be enabled through EA.

To map *.example.com to a specific subdirectory use a config similar to below.

<VirtualHost <IP>:80>
  ServerName example.com
  ServerAlias *.example.com
  VirtualDocumentRoot /home/username/public_html/stores/%1
  ServerAdmin webmaster@example.com
  UseCanonicalName Off
  CustomLog /usr/local/apache/domlogs/%1.example.com combined

  <IfModule log_config_module>
    <IfModule logio_module>
      CustomLog /usr/local/apache/domlogs/%1.example.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
    </IfModule>
  </IfModule>

  <IfModule include_module>
    <Directory "/home/username/public_html/stores/%1">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from all
    </Directory>
  </IfModule>
</VirtualHost>

You can also map subdomains using a method similar to rewrite rules.

<VirtualHost se.rv.er.ip.ad.re.ss>
        ServerAdmin info@servername.com
        DocumentRoot /var/www
        ErrorLog /var/log/apache/apache-error.log
        TransferLog /var/log/apache/apache.log
        ServerName www.servername.com
        ServerAlias *.servername.com
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www.* [NC]
        RewriteCond %{HTTP_HOST} ^([^\.]+)\.servername\.com
        RewriteRule ^/$ http://www.servername.com/user.php?user=%1
</VirtualHost>