.htaccess rewrite rule to prevent caching of css,

2019-07-18 05:14发布

I've tried the ?ver=478459 stuff to help prevent browsers from caching some files like CSS, Images, JavaScript. And Since I've come to realize its only so helpful. Does't appear to work every time. Example, I have to VPN into work on occasion, and for whatever reason even when changing the parameters at the end of a file like above mentioned files will remain in the cache until I clear it. I'm figuring the parameter might not be sticking for some reason cause I am running through a proxy. But, it does't always seem to be the case either.

Anyway. I am trying to figure out, if there is a way I can through htaccess provide a rewrite_rule for my js, css, img files and if the URL provided is I dont know lets say

/scripts/jquery/__ver<version number>__/filename.js
/scripts/__ver<version number>__/filename2.js
/scripts/3.0.x/__ver<version number>__/filename2.js
/styles/jquery-ui/__ver<version number>__/filename.css
/styles/__ver<version number>__/filename2.css
/img/__ver<version number>__/something.png
/img/dir/dir/dir/__ver<version number>__/something-else.png

essentially where the rewrite rule is looking for this __ver<version number>__ specifically and <version number> is either a 3 dot versioning logic or a md5 of some sort.. or something either way basically looking for _ver*_

Where when this is found, the rewrite rule would remove it and user the path without that part

2条回答
Ridiculous、
2楼-- · 2019-07-18 06:07

You can explicitly disable caching in .htaccess for chosen file extensions:

<FilesMatch "\.(png|jpe?g|gif|js|css)$">
   FileETag None
   <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Tue, 14 Jan 1975 01:00:00 GMT"
   </ifModule>
</FilesMatch>
查看更多
迷人小祖宗
3楼-- · 2019-07-18 06:13

How about somethin glike this:

RewriteEngine On
RewriteRule ^(.*)__ver([0-9]+)__(.*)$ /$1$2?ver=$1 [L,QSA]

You can add that to the htaccess file in your document root and it'll strip out the __ver*__ part of the URI and add a ver= query string parameter.

查看更多
登录 后发表回答