Search code examples
phpapachecentossubdomainvirtualhost

Share assets folder between two subdomain's document roots


I have two web apps that share the same assets (js, css, img etc.) but use different subdomains/document roots.

I'm trying to figure out how i can share one assets folder between the two document roots, so i can avoid having to change the assets folder for each subdomain after i apply changes to the assets.

Thanks for your time.

Current Structure:

www/html
    ├─ subdomain1
    │   └─ assets
    │   └─ index.php
    └─ subdomain2
        └─ assets
        └─ index.php

Desired Structure:

www/html
    ├─ assets
    ├─ subdomain1
    │   └─ index.php
    └─ subdomain2
        └─ index.php

VHost Confs:

subdomain one:

<VirtualHost *:80>
  ServerName www.one.mydomain.com
  ServerAlias one.mydomain.com
  ServerAdmin admin@example.com
  DocumentRoot /var/www/html/subdomain1/

<Directory /var/www/html/subdomain1/>
   Options -Indexes +FollowSymLinks
   AllowOverride All
   DirectoryIndex index.php
</Directory>

  ErrorLog /var/www/html/logs/subdomain1-error.log
  CustomLog /var/www/html/logs/subdomain1-access.log combined
</VirtualHost>

subdomain two:

<VirtualHost *:80>
  ServerName www.two.mydomain.com
  ServerAlias two.mydomain.com
  ServerAdmin admin@example.com
  DocumentRoot /var/www/html/subdomain2/

<Directory /var/www/html/subdomain2/>
   Options -Indexes +FollowSymLinks
   AllowOverride All
   DirectoryIndex index.php
</Directory>

  ErrorLog /var/www/html/logs/subdomain2-error.log
  CustomLog /var/www/html/logs/subdomain2-access.log combined
</VirtualHost>

Solution

  • In the config for the second host, you could use Apache's mod_alias to define an alias to the "primary" asset directory:

    <VirtualHost *:80>
        ServerName www.two.mydomain.com
        ...
        <Location "/assets">
            Alias "/var/www/html/subdomain1/assets"
        </Location>
    

    Then just remove the duplicated directory /var/www/html/subdomain2/assets.

    Alternately, you could leave the config as is, remove the second dir, and then symlink to the first:

    mv /var/www/html/subdomain2/assets /var/www/html/subdomain1/assets.old
    ln -s /var/www/html/subdomain1/assets /var/www/html/subdomain2
    

    Note however that either method could make deployments/development tricky, as you're essentially locking them to always have the same versions of the same files. Ultimately, it's likely best to leave them as duplicates and instead address your deployment mechanism.