我有一个简单的问题。我有webdirectory /css
,里面是文件style.css
。我已经手动将此文件压缩并保存为style.css.gz
。我想节省CPU周期,以免每次请求都压缩CSS文件。如何配置Apache以查找并提供.gz
文件,而不是一遍又一遍地压缩.css
文件?
注意:我不希望Apache自己创建.gz
文件。在我的场景中,我必须手动创建.css.gz
文件-在特定请求上使用PHP。
最佳答案
一些RewriteRule应该可以很好地处理它。
在Drupal配置文件中,我发现:
# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz
#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
这将使工作。如果您无权访问apache配置或要降低Web服务器的速度,则可以将其放在
<Directory foo/>
节或.htaccess文件中。关于apache - 如何强制Apache使用CSS和JS文件的手动预压缩gz文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9076752/