如果通过.htaccess更新,则重定向到精简版本 [英] Redirect to minified version if is updated via .htaccess
问题描述
有一种方法可以通过 .htaccess
检查 .css
还是。 js
有自己的缩小版本( .min.css
和 .min.js
,并重定向到该文件,但需要具有相同的修改时间。
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.
例如:
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
我的想法是:有人访问文件时。 css
, .htaccess
将检查 file.min.css
是否可用并具有相同的修改时间。如果是,它将返回(通过重写,重定向),而不是 file.css
。
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
.
但是,如果我访问 file.js
,甚至是现有的 file.min.js
,也就是。 htaccess
将返回 file.js
,因为 file.min.js
已过时(没有
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).
是否可能仅 与 .htaccess
?
推荐答案
对于您的第一个要求,请在您的 DOCUMENT_ROOT / .htaccess
中尝试此规则文件:
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]
但是 mod_rewrite
无法检查css / js文件的修改时间戳在此基础上做出决定。您可能需要在服务器端代码中进行处理。
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.
这篇关于如果通过.htaccess更新,则重定向到精简版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!