Skip to content

Instantly share code, notes, and snippets.

@logsol
Created March 28, 2012 21:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save logsol/2230673 to your computer and use it in GitHub Desktop.
Save logsol/2230673 to your computer and use it in GitHub Desktop.
Apache - automatically route all subdomains to their related directory
===========================================================================
APACHE
How to get "anysubdomain.mydomain.com" automatically routed to a directory?
===========================================================================
1.) enable mod_vhost_alias (create symlink in mods-enabled)
2.) create the following vhost configuration file /etc/apache/sites-available/subdomains
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
VirtualDocumentRoot /path/to/webspace/%1/public
SetEnv APPLICATION_ENV dev
</VirtualHost>
3.) to enable, create symlinks for subdomains in /sites-enabled
4.) restart apache
Pro tipp no. 1:
I set VirtualDocumentRoot to /var/www/ where I only have a load of soft links to my actual projects.
Pro tipp no. 2:
%1 will stands for the name of the subdomain only meaning, if someone opens sub.mydomain.com, apache will look for /path/to/webspace/sub/public. if you use %0 the whole domain name is used /path/to/webspace/sub.mydomain.com/public
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment