我正在尝试让 Google PageSpeed 满意,但我的 .htaccess 技能有限。我尤其难以处理浏览器和代理缓存。有人有我可以使用的模板吗?
以下是我目前所进行的压缩:
# JavaScript MIME type issues:
# 1. Apache uses "application/javascript": http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
# 2. IIS uses "application/x-javascript": http://technet.microsoft.com/en-us/library/bb742440.aspx
# 3. SVG specification says it is text/ecmascript: http://www.w3.org/TR/2001/REC-SVG-20010904/script.html#ScriptElement
# 4. HTML specification says it is text/javascript: http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#h-18.2.2.2
# 5. "text/ecmascript" and "text/javascript" are considered obsolete: http://www.rfc-editor.org/rfc/rfc4329.txt
#==================================================================================================
# Compression: http://code.google.com/speed/page-speed/docs/payload.html#GzipCompression
#==================================================================================================
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# The following MIME types are in the process of registration
AddOutputFilterByType DEFLATE application/xslt+xml
AddOutputFilterByType DEFLATE image/svg+xml
# The following MIME types are NOT registered
AddOutputFilterByType DEFLATE application/mathml+xml
AddOutputFilterByType DEFLATE application/rss+xml
# Compress JavaScript; make sure to list all possible MIME types for JavaScript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/ecmascript
AddOutputFilterByType DEFLATE text/javascript
</IfModule>
#--------------------------------------------------------------------------------------------------
答案1
这是一个你可以使用的模板。我从某人那里得到它,不知道原作者是谁,但它很可能是 yslow 插件的输出(http://developer.yahoo.com/yslow/)。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
</IfModule>
# if you don't use filenames to version, lower the css and js to something like
# "access plus 1 week" or so
<IfModule mod_expires.c>
Header set cache-control: public
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.manifest needs re-reqeusts in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# your document html
ExpiresByType text/html "access"
# rss feed
ExpiresByType application/rss+xml "access plus 1 hour"
# favicon (cannot be renamed)
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
# favicon (cannot be renamed)
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
# media: images, video, audio
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
# webfonts
ExpiresByType font/ttf "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
# css and javascript
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
</IfModule>
# Since we're sending far-future expires, we don't need ETags for
# static content.
# developer.yahoo.com/performance/rules.html#etags
FileETag None
# use utf-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
# force utf-8 for a number of file formats
AddCharset utf-8 .html .css .js .xml .json .rss
答案2
您的语法看起来不错。有些人可能会说我这样做很老套,但我使用了此处显示的 apache 示例配置:
此外,我强烈建议将这些规则放在 apache 配置文件中,而不是 .htaccess 文件中。这是因为当请求到达时,apache 必须四处寻找 .htaccess 文件,读取它们,并动态处理它们的配置。如果您将配置放在主 apache 配置文件中,它会在 apache 首次启动时编译它们。
在每秒有大量请求的非常繁忙的网站上,使用 .htaccess 文件会很快降低网站的速度。