页面没有正确缓存,响应标题似乎错误...如何解决?

时间:2011-06-07 18:06:22

标签: .htaccess http-headers mod-expires

好吧,我真的不知道如何提出问题,但我的问题或者至少我认为问题是这样的:

我正在处理的博客加载速度非常慢,所以我做了正常的事情以加快速度,例如添加gzip和禁用所有插件等等,但它没有帮助。所以我查看了Firebug中发送的标题,并注意到页面的原始请求正在进行,而其余请求正常加载。

以下是给定页面的响应/请求标头:

请求标题

Host:               dev.mydomain.com
User-Agent:         Mozilla/5.0... ...Firefox/3.6.17
Accept:             text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:    en-us,en;q=0.5
Accept-Encoding:    gzip,deflate
Accept-Charset:     ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive:         115
Connection:         keep-alive
Referer:            http://dev.mydomain.com/blog/2011/05/06/hello-world/
Cookie:             Cookie data...

响应标题

Date:               Tue, 07 Jun 2011 17:37:42 GMT
Server:             Apache
X-Pingback:         http://dev.mydomains.com/blog/xmlrpc.php
Expires:            Thu, 19 Nov 1981 08:52:00 GMT
Last-Modified:      Tue, 07 Jun 2011 17:37:59 GMT
Cache-Control:      no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:             no-cache
Vary:               Accept-Encoding
Content-Encoding:   gzip
Content-Length:     4981
Connection:         close
Content-Type:       text/html; charset=UTF-8

显然这里出现了问题,因为没有缓存,连接设置为关闭,过期时间是30年前。

这是我的.htaccess文件,我设置过期标题等。

的.htaccess

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-javascript

# Or, compress certain file types by extension:
<Files *.html|*.php>
SetOutputFilter DEFLATE
</Files>

ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/javascript A2592000

FileETag none

AddType text/x-component .htc
AddType image/x-icon .ico 

AddHandler application/x-httpd-php .php .html

DirectoryIndex index.php
allow from all
RewriteEngine on

RewriteBase /  
RewriteCond %{REQUEST_URI} ^/blog/
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ /blog/index.php [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

## Ignore CSS, Scripts and Images
RewriteRule !\.(xml|swf|js|ico|gif|jpg|png|css|htc)$ index.php [L]

任何帮助,如果非常感激。

BTW,我在CentOS 5上运行PHP 5.2.10 / MySQL 5.1上的WordPress 3.1.1。*

1 个答案:

答案 0 :(得分:0)

我正在与同样的问题作斗争,我发现的一件事是你的.htaccess文件只有你的网站主机提供的权限一样好。您是托管自己的网站还是拥有网站托管公司?

如果您要托管自己的站点,则需要确保正确设置“允许覆盖”指令,以便.htaccess规则可以正常工作。更好的是,设置httpd.config文件也可以为你完成工作。

但是,如果您与一家网站托管公司合作,那么您需要向他们询问您可以使用哪些指令(您编写的那些.htaccess规则)?如果您的webhost将Allow Override设置为NONE,则将忽略.htaccess文件。

我搜索到的所有地方都表明,在遥远的未来设置的Expires标头是最好的:当它在过去(1981年11月)设置时,它会强制浏览器每次都获取新的页面/图像。

我建议的最后一件事是仔细检查你的.htaccess规则,这里有一个对你有用的集合作为起点:

#BEGIN htaccess
#Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>

#Protect the htaccess file
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>

# Disable directory browsing
Options All -Indexes

# Enable the following of symlinks
Options +FollowSymLinks

<IfModule mod_headers.c>
# No ETags, No Pragma
Header unset Pragma
Header unset ETag
# Make sure proxies deliver correct content
Header append Vary User-Agent env=!dont-vary
# Ensure proxies deliver compressed content correctly
Header append Vary Accept-Encoding
</IfModule>

# Set up Cache Control headers
ExpiresActive On
# Default - Set http header to expire everything 1 week from last access, set must-revalidate
ExpiresDefault A604800
Header append Cache-Control: "max-age=3600, must-revalidate"
# Apply a customized Cache-Control header to frequently-updated files
<FilesMatch "^(test¦eval)\.html$">
ExpiresDefault A1
Header unset Cache-Control:
Header append Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
<FilesMatch "^robots\.txt$">
ExpiresDefault A7200
</FilesMatch>

ExpiresByType image/x-icon A14515200

# Set up caching on media files for 1 month
<FilesMatch "\.(gif|jpg|JPG|jpeg|png|PNG|swf)$">
  ExpiresDefault A2419200
</FilesMatch>

# Set up caching on commonly updated files 1 month
<FilesMatch "\.(xml|txt|html|js|css)$">
  ExpiresDefault A2419200
</FilesMatch>

<FilesMatch "\.(ico|gif|jpg|JPG|jpeg|png|PNG|css|js|html?|xml|txt)$">
FileETag None
</FilesMatch>

<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|text|html)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

希望这有所帮助 - 祝你好运!

Lightfoot