Skip to content

Instantly share code, notes, and snippets.

@EllieTheYeen
Forked from logsol/README
Last active November 25, 2023 15:53
Show Gist options
  • Save EllieTheYeen/daf340fff3a0603fd2e4158df36db2f5 to your computer and use it in GitHub Desktop.
Save EllieTheYeen/daf340fff3a0603fd2e4158df36db2f5 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