首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于缓存的优化.htaccess?

用于缓存的优化.htaccess?
EN

Server Fault用户
提问于 2010-10-17 13:44:29
回答 2查看 1.7K关注 0票数 2

我试图让Google PageSpeed开心,但我的.htaccess技能有限。我在浏览器和代理缓存方面特别困难。有人有我可以使用的模板吗?

到目前为止,我的压缩功能如下:

代码语言:javascript
复制
# 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>
#--------------------------------------------------------------------------------------------------
EN

回答 2

Server Fault用户

发布于 2010-11-24 19:04:14

这是一个你可以使用的模板。我是从某个人那里得到的,不知道原作者是谁,但它很可能是yslow插件(http://developer.yahoo.com/yslow/)的输出。

代码语言:javascript
复制
<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
EN

Server Fault用户

发布于 2010-10-17 18:38:28

你的语法看起来不错。有些人可能会说我这样做很老套,但我使用了apache的示例配置,如下所示:

  • http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#recommended

此外,我强烈建议将这些规则放在文件中而不是.htaccess文件中。这是因为当请求传入时,apache必须搜索您的.htaccess文件,读取它们,并动态地处理它们的配置。如果您将配置放在您的主要apache配置文件中,它将在apache首次启动时对它们进行编译。

在每秒有大量请求的繁忙站点上,使用.htaccess文件可以非常快地降低站点的速度。

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/191818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档