.htaccess规则适用于某些页面,但不适用于其他页面

时间:2014-09-14 06:06:56

标签: apache .htaccess cache-control mod-deflate

我正在尝试使用Google PageSpeed Insights来加速我的网站 - www.edmhunters.com。

首先,这是我的.htaccess文件的样子

# -----------------------------------------------------------------------
# Caching for 1 Year
# -----------------------------------------------------------------------
<FilesMatch "\.(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# -----------------------------------------------------------------------
# Caching for 1 Week
# -----------------------------------------------------------------------
<FilesMatch "\.(jpg|png|gif|css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

# -----------------------------------------------------------------------
# Defining MIME types to ensure the web server actually knows about them
# -----------------------------------------------------------------------
<IfModule mod_mime.c>
    AddType application/javascript          js
    AddType application/vnd.ms-fontobject   eot
    AddType application/x-font-ttf          ttf ttc
    AddType font/opentype                   otf
    AddType application/x-font-woff         woff
    AddType image/svg+xml                   svg svgz 
    AddEncoding gzip                        svgz
</Ifmodule>

# -----------------------------------------------------------------------
# Compressing output
# -----------------------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</Ifmodule>

Cache-Control标题奇怪地仅适用于媒体文件而非静态文件。

EG。 redbot.org上的http://www.edmhunters.com/media/dj/images/deadmau5_2.jpg输出如下所示

 HTTP/1.1 200 OK
    Date: Sun, 14 Sep 2014 06:01:54 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Thu, 11 Sep 2014 13:52:29 GMT
    ETag: "7391-502ca7aba5a6e"
    Accept-Ranges: bytes
    Content-Length: 29585
    Cache-Control: max-age=604800, public
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: image/jpeg

虽然http://www.edmhunters.com/static/img/bp-logo.9bf55788f1b6.jpg的输出看起来像这样

HTTP/1.1 200 OK
    Date: Sun, 14 Sep 2014 05:43:28 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Tue, 26 Aug 2014 05:43:32 GMT
    ETag: 1409031812.62
    Content-Length: 2592
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: image/jpeg

同样根据Google PageSpeed Insights for my homepage,HTML缩小了,而Google PageSpeed Insights for another page表示我的HTML没有缩小。

为什么会出现这种不同的行为?

1 个答案:

答案 0 :(得分:0)

我在使用Apache时使用Django提供静态文件。通过Apache提供我的静态文件解决了这个问题。