This is actually the problem: we've plenty of Javascripts and a lot of CSS files, which we'd prefer to be serving minified. Minification is simple: setup the YUI Compressor, run an Ant task, also it spits out minified files, which we save near the originals.

Therefore we finish track of the next directory structure somewhere within our DocumentRoot:











Ok now what we want is the fact that Apache serve files in the min subdirectory, and fallback to serving uncompressed files, if their minified versions aren't available. The final problem may be the one I am unable to solve.

For instance: suppose there exists a request to — within this situation Apache should send items in /js/min/foo-min.js. There's no minified quux.js, so request to /js/quux.js returns /js/quux.js itself, not 404. Finally, if there's no /js/fred.js, it will finish track of 404.

Really, I am setting build scripts in a way that unminified files aren't used around the production server, but this configuration most likely will be helpful with an integration server as well as on development machines.

This is actually the configuration that finally labored:


RewriteEngine On

RewriteBase /js

RewriteCond %{REQUEST_URI} ^/js/((.+)\.js)$
RewriteCond %{DOCUMENT_ROOT}/js/min/%2-min.js -f
RewriteRule ^(.+)$ min/%2-min.js [L]

Same for css directory.

You should use RewriteCond to identify the existence of a minified file:

RewriteCond %{REQUESTURI} ^/js/(.*\.js)
RewriteCond js/min/%1 -f
RewriteRule %1 min/%1 [L]

Can you really improve your build scripts? If that's the case, you are able to configure these to minify the files and provide them exactly the same file title, only when provided the correct flag, e.g. ant productionDeploy rather than ant integrationDeploy. This way the minification process is totally transparent to everything except the build script.