使用PHP和.htaccess将修改后的时间添加到文件名中以强制进行新的缓存 [英] Add time modified to filename with PHP and .htaccess to force fresh caching

查看:84
本文介绍了使用PHP和.htaccess将修改后的时间添加到文件名中以强制进行新的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循此规则( http://www.electrictoolbox。 com / force-reload-css-javascript-unique-filenames / )教程通过将修改后的时间添加到文件名中来动态命名我的CSS和javascript文件来强制更新缓存。这是在开发期间使用的,因为我在托管服务器端缓存文件时遇到问题。我已经确认mod_rewrite已经打开并且正在读取htaccess,我肯定是在apache服务器上。我的脚本没有任何错误,但是当我使用开发人员工具检查源代码时,文件名没有更改。测试页面为www.diysoakwells.com.au/cart2.php。

I have followed this (http://www.electrictoolbox.com/force-reload-css-javascript-unique-filenames/) tutorial to force an update of the caching by dynamically naming my css and javascript files by adding the time modified to the filename. This is for use during development as I am having issues with my hosting caching my files serverside. I have confirmed that mod_rewrite is on and htaccess is being read, I am definately on an apache server. I get no errors in my script but when I inspect the source with developer tools the filename has not changed. The test page is www.diysoakwells.com.au/cart2.php.

.htaccess:

.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^diysoakwells.com.au$
RewriteRule (.*) http://www.diysoakwells.com.au/$1 [R=301,L]
RewriteRule ^(css|js)/(.*)\.[0-9]+\.(.*)$ /$1/$2.$3 [L]


<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

# Don’t compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>

</IfModule> 

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

PHP

<link rel="stylesheet" type="text/css" href="/css/styles2.1<?php echo filemtime('/css/styles2.1.css'); ?>.css" />


<script language="javascript" src="/js/simpleCart_verify<?php echo filemtime('/js/simpleCart_verify.js'); ?>.js"></script>

我已经尝试了评论中的解决方案,但对我来说一切都很好,是否有人提出任何建议?谢谢。

I have tried the solution in the comments but it all looks fine to me, does anyone have any suggestions? Thanks.

推荐答案

我认为您正在使它变得不必要地复杂。您可以添加查询变量来代替修改文件名。这也将强制重新加载,并且无需重写资产文件名:

I think you are making it unnecessarily complicated. Instead of modifying the filename, you can add a query variable. That would force a reload as well and requires no rewriting of your assets filenames:

<link rel="stylesheet" type="text/css" href="/css/styles2.1.css?ts=<?php echo filemtime('/path/to/css/styles2.1.css'); ?>" />
// etc.

只要发送相同的查询变量,结果也会被缓存。

And as long as you send the same query variable, the result will be cached as well.

这篇关于使用PHP和.htaccess将修改后的时间添加到文件名中以强制进行新的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆