0

I am trying to append a query string to the end of all .js files, without actually making a code change. The purpose of this would be so static files get pulled from the server rather than the cache when they are changed so we don't have any stale static files.

So when my html says

<script type="text/javascript" src="file1.js">

I want it to actually pull

<script type="text/javascript" src="file1.js?v=1">

from the server. Is this possible?

So far I have:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^(.*)\.js$ /$1.js?v=1 [L,R]

But I don't think it's quite doing what I want it to...

Qantas 94 Heavy
  • 14,790
  • 31
  • 61
  • 78
  • 2
    If the resource is cached with an expiry date, the browser will never make the request in the first place. Why not simply set the caching rules properly instead? – Pekka Sep 26 '13 at 01:52
  • @Pekka웃: the questioner might not understand how the caching rules are actually applied - you might want to explain that in an answer. – Qantas 94 Heavy Sep 26 '13 at 01:56
  • @Qantas yeah, in the process of looking for a link – Pekka Sep 26 '13 at 01:56
  • possible duplicate of [Prevent http file caching in Apache httpd (MAMP)](http://stackoverflow.com/q/11532636) – Pekka Sep 26 '13 at 01:56
  • I don't want to not cache them, and I don't want to set them to expire after a certain amount of time. I would set them to never expire and when the files are changed, the server would effectively change their name and recache the new file. Something similar to this [force-reload-css-javascript-unique-filenames](http://www.electrictoolbox.com/force-reload-css-javascript-unique-filenames/) but without making all the changes in the html. Just making an apache change. – user2817582 Sep 26 '13 at 02:24
  • You should change the file name, or path to the file, rather than appending a query string variable - it's a more reliable way to bust the cache, and better for when you do want the file cached. – Tom Pietrosanti Jan 22 '14 at 20:08

2 Answers2

0

This doesn't make sense: if the resource is cached with an expiry date, the browser will never make the request in the first place.

The much more straighforward way is to set up proper caching rules for the JS file as outlined here: How to prevent http file caching in Apache httpd (MAMP), adjusting the filesMatch directive to your liking.

Community
  • 1
  • 1
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
0

This will double the number of requests for the files (all .js files in this case), but it should work otherwise.

I'm using a directory (effectively modifying the filename), rather than a query string because that is the preferred technique. [1][2]

First, redirect to a different folder name

ReWriteRule ^(.*\.js)$ /rev/000/$1 [L,R]

Replace the 000 part with your revision number (you should be able to automate this)

The L flag stops processing rewrites, and the R does an HTTP redirect, initiating a second request to your server using the new file name.

Then catch requests tagged using the new "folder" and point them back to the original file using a rewrite rule that does not invoke another request (your first rule will be passed over this time around)

ReWriteRule ^/rev/[0-9]+/(.*)$ $1
Tom Pietrosanti
  • 3,774
  • 2
  • 17
  • 26