DrKHunter
DrKHunter

Reputation: 424

Conditional URL rewrites in PHP

I've written a custom CMS that can host multiple client/sites. The sites and their various parts are stored within a /client-name/site-name directory structure.

The htaccess passes every request through the index that doesn't exist.

It's not ideal to have '/clients/client-name/site-name/images/img.jpg', so to allow just '/images/img.jpg' I've captured the request and I then effectively serve the real location of the file; headers and all. I'm doing this for CSS, JS etc too.

This has worked fine, however I'm now noticing that this doesn't allow the files to be cached properly, so every time a page is served it loads the whole thing again which is a pain and looks awful.

Does anyone know a way around this? Apache config or htaccess solution would be fine, but it needs to know the location of the file, which is from the DB worked out by the index of the CMS.

Help!

Upvotes: 1

Views: 131

Answers (1)

Fenton
Fenton

Reputation: 251242

I am serving up JavaScript through a PHP script (which I use to combine / minify the JavaScript so I serve a single file).

I use the following headers to ensure it gets cached:

header('Content-type: text/javascript');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 6048000));

In my case, the original download takes 982ms. After this it takes just 87ms to fetch from the cache for each subsequent request (Firefox 10.0.1)

Upvotes: 2

Related Questions