.htaccess - 用于利用浏览器缓存的 htaccess 不起作用

标签 .htaccess http caching http-headers browser-cache

我在利用浏览器缓存方面遇到问题。 我已经创建了这个 .htaccess 文件并在我的服务器上使用它,但它似乎没有工作,我从这里“http://www.samaxes.com/2011/05/improving-web-performance-with-apache-and-htaccess/”复制了它。 我很可能会犯一个基本错误,所以任何帮助都会很好。 这里有一些细节。我正在研究一个子域,我们称它为“sub”,主域为“example”。 所以我想要 http://sub.example.com/ 中的所有内容htaccess 文件的工作。 在我的服务器上,我将 .htaccess 文件放在 sub 的主目录中,例如,该目录与主目录分开。

我正在运行同时启用了 mod_headers 和 mod_expires 的 apache2 服务器。

干杯。

<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
AddOutputFilterByType DEFLATE font/truetype font/opentype
</ifModule>
<ifModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 5 seconds"
 ExpiresByType image/x-icon "access plus 2592000 seconds"
 ExpiresByType image/jpeg "access plus 2592000 seconds"
 ExpiresByType image/png "access plus 2592000 seconds"
 ExpiresByType image/gif "access plus 2592000 seconds"
 ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
 ExpiresByType text/css "access plus 604800 seconds"
 ExpiresByType text/javascript "access plus 216000 seconds"
 ExpiresByType application/javascript "access plus 216000 seconds"
 ExpiresByType application/x-javascript "access plus 216000 seconds"
 ExpiresByType text/html "access plus 600 seconds"
 ExpiresByType application/xhtml+xml "access plus 600 seconds"
 </ifModule>
 <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
 Header set Cache-Control "max-age=2592000, private"
 Header set Expires "Sun, 17 July 2013 20:00:00 GMT"
 </filesMatch>
 <filesMatch "\\.(css|css.gz)$">
 Header set Cache-Control "max-age=604800, private" 
 </filesMatch>
 <filesMatch "\\.(js|js.gz)$">
 Header set Cache-Control "max-age=604800, private"
 </filesMatch>
 <filesMatch "\\.(xml|txt)$">
 Header set Cache-Control "max-age=216000, private, must-revalidate"
 </filesMatch>
 <filesMatch "\\.(html|htm)$">
 Header set Cache-Control "max-age=7200, private, must-revalidate"
 </filesMatch>
 FileETag None

最佳答案

看起来很乱。

你也有多余的规则,比如

ExpiresByType application/javascript "访问加上 216000 秒"

<filesMatch "\\.(js|js.gz)$">
Header set Cache-Control "max-age=604800, private"
</filesMatch>

尝试使用 html5-boilerplate 中更清晰的(和最新的)规则或检查所有 h5bp server configs

关于.htaccess - 用于利用浏览器缓存的 htaccess 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728678/

相关文章:

java - 考虑到旧 key 的 Guava 缓存

html - IIS/AngularJS 单页应用程序 - 在应用程序中导航时如何防止单个 index.html 的页面缓存

.htaccess - htaccess 将子域指向文件夹

.htaccess - 亚马逊弹性 beantalk : Use nginx/apache to forward subdomains to subfolders

javascript - EventSource 连接中的自定义 header

java - 如何防止 Java 中的 SocketInputStream.socketRead0 挂起?

php - 什么是缓存标记?

php - htaccess 中的 SetEnvIf Referer

.htaccess - 将 IP 重定向或指向另一台服务器

typescript - 从 Typescript 中的 http 响应获取 header 值