Murray Rowan
Murray Rowan

Reputation: 3715

Best practices for maintaining assets accross (sub)domains?

My webserver filesystem is equivalent to the following:

/master-domain.tld
/master-domain.tld/assets/js/foo.js
/master-domain.tld/assets/css/bar.css

/sub1.master-domain.tld
/sub1.master-domain.tld/assets/js/foo.js

/sub2.master-domain.tld
/sub1.master-domain.tld/assets/css/bar.css
...

What I'd like to know is how to serve my common static assets (hosted on the master domain) to my subdomains-- reason being that I then only have to maintain a single set of 'master' assets, rather than update/copy any files to all other subdomains each time I edit them. Obviously this can be achieved using absolute URLs, but my aim is to avoid these so that my local/dev system needn't make any remote calls while I'm designing/debugging.

Currently, I have a mod_rewrite + symbolic link + php script combo set up for each subdomain, linking any calls to non-existent local assets to the master assets. e.g. calling "/sub1.master-domain.tld/assets/css/bar.css" will retrieve the bar.css file hosted on the master domain since a local version of bar.css does not exist. Furthermore, calling "/sub1.master-domain.tld/assets/js/foo.js" would serve the local version of foo.js, since it does exist.

But my current method seems to be hindering the performance of my page loads, and I wonder if there is a better (still modular) approach to solving this problem.

Any tips? Am I going about this in completely the wrong way?

Upvotes: 4

Views: 415

Answers (3)

Matt S
Matt S

Reputation: 15364

Symbolic links should be all that's required if they're all on the same server. This should cause no performance hits at all. E.g.

/sub1.master-domain.tld/assets -> /master-domain.tld/assets

If your subdomains are served from multiple servers, I would set up a mod_rewrite rule with 302 redirects to the complete URL on the master domain. On that master domain server I would set mod_expires so the assets are cached, avoiding most subsequent requests to the same "missing" assets on the subdomains.

Upvotes: 1

genesis
genesis

Reputation: 50966

I'd use reserved static. subdomain for this purpose and aim all request on that one.

Upvotes: 0

Gerry
Gerry

Reputation: 6012

If they are on the same machine, why not point different virtual host aliases to the same document root?

Upvotes: 0

Related Questions