David Rodrigues
David Rodrigues

Reputation: 12552

Redirect to minified version if is updated via .htaccess

There are a way, via .htaccess to check if .css or .js has your own minified version (.min.css and .min.js, respectively) and redirect to this file, but need to have the same modified time.

For instance:

FILE            MODIFIED TIME
/file.css       01-15-2014T19:28:12
/file.min.css   01-15-2014T19:28:12
/file.js        01-15-2014T19:28:12
/file.min.js    01-15-2014T19:27:47

My idea is: when someone access the file.css, the .htaccess will check if file.min.css is available and have the same modified time. If yes, it'll returned (via rewrite, redirect), instead of file.css.

But, if I access file.js, even existing the file.min.js, the .htaccess will return file.js because file.min.js is outdated (not have same date/hour).

Is it possible only with .htaccess?

Upvotes: 0

Views: 934

Answers (1)

anubhava
anubhava

Reputation: 785781

For your first requirement try this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

# if a min version is available then load it
RewriteCond %{DOCUMENT_ROOT}/$1.min.$2 -f
RewriteRule ^([^.]+)\.(css|js)$ /$1.min.$2 [L,NC]

However mod_rewrite cannot check modification timestamp of your css/js files to make decision based on that. You might need to handle that in your server side code.

Upvotes: 2

Related Questions