如何强制Apache使用手动预压缩的CSS和JS文件的gz文件? [英] How to force Apache to use manually pre-compressed gz file of CSS and JS files?
问题描述
我有一个简单的问题.我有 webdirectory /css
里面是文件 style.css
.我已手动 gzip 压缩此文件并将其另存为 style.css.gz
.我想节省 CPU 周期,以免在每次请求时都压缩 CSS 文件.如何配置 Apache 以查找此 .gz
文件并提供它,而不是一遍又一遍地压缩 .css
文件?
I have simple question. I have webdirectory /css
and inside is file style.css
. I have manually gzipped this file and saved it as style.css.gz
. I want to save CPU cycles to not have CSS file compressed at each request. How do I configure Apache to look for this .gz
file and serve it instead of compressing .css
file over and over again ?
注意:我不希望 Apache 自己创建 .gz
文件.在我的场景中,我必须手动创建 .css.gz
文件 - 在特定请求上使用 PHP.
Note: I don't want Apache to create .gz
file itself. In my scenario I have to create .css.gz
file manually - using PHP on specific requests.
推荐答案
一些 RewriteRule 应该可以很好地处理这个问题.
Some RewriteRule should handle that quite well.
在我发现的 Drupal 配置文件中:
In a Drupal configuration file I found:
# 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 配置或想要降低网络服务器的速度,您可以将其放在 <Directory foo/>
部分或 .htaccess 文件中.
This will make the job. You can put that either in a <Directory foo/>
section or in a .htaccess file if you do not have access to apache configuration or if you want to slowdown your webserver.
这篇关于如何强制Apache使用手动预压缩的CSS和JS文件的gz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!