1

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

3
  • 1
    Why do a rewrite? I would propose sending appropriate no-cache headers. Commented Sep 10, 2013 at 17:11
  • Because I am stuck with a static HTML file, that has no server side code capabilities. So passing headers on the fly at point of render won't work for me. Also some browsers tend to just ignore certain headers, or treat them different, still caching the scripts/images/css (tested live on various os's from XP to 8, linux distro's, and Mac's.. with various versions of all browsers from IE 7 to 10, Safari, Chrome older and newer, Firefox 3.5+ all the way up to current) Not all are created equal or handle equally with headers, this rewrite is my last ditch effort in combination with other tactics Commented Sep 10, 2013 at 17:43
  • 1
    You can configure header for static files. See answer from anughava below for an example of how to use mod_headers to do this. Commented Sep 10, 2013 at 18:34

2 Answers 2

6

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.