Help - Search - Members - Calendar
Full Version: mod_rewrite Version Control for frequently updated files
HTMLHelp Forums > Programming > Web Server Configuration
askapache
Web Developers sometimes use file.ext?v=001 as a version control system so they can force visitors to use an updated file. This is so bad.

Let's look at how we can accomplish the same thing (and much better caching and site speed) using a simple RewriteRule.

So I have 2 files to demonstrate. /c/apache.css and /j/apache.js - now I have my htaccess setup so that .js and .css files are cached forever, so what do I do when I make an update to either of those files?

I create a RewriteRule in my .htaccess file so that /j/apache-001.js points to /j/apache.js, but the visitors browser only sees /j/apache-001.js, and when I update /j/apache.js, I only have to change the links in my XHTML to /j/apache-002.js and that forces the browser to use the updated file! As long as the HTML isn't being cached too aggressively, this system is great and I use it on all my clients sites.

The htaccess
CODE
RewriteEngine On
RewriteBase /
RewriteRule ^j/apache-([0-9]+).js$ /j/apache.js [L]
RewriteRule ^c/apache-([0-9]+).css$ /c/apache.css [L]
The XHTML
CODE
<link href="http://z.askapache.com/c/apache-004.css" rel="stylesheet" type="text/css" />
<script src="http://z.askapache.com/j/apache-004.js" type="text/javascript"></script>


More robust/complex example
CODE
RewriteEngine On
RewriteBase /
RewriteRule ^([cij]+)(/?[a-z]*)/([a-z]+)-([0-9]+)\.([a-z]+)$ /$1$2/$3.$5 [L]
The XHTML
CODE
<link href="http://z.askapache.com/c/anything-007.css" rel="stylesheet" type="text/css" />
<script src="http://z.askapache.com/j/anything-007.js" type="text/javascript"></script>


Read the Original Article to learn more and combine this method with htaccess caching
Darin McGrew
QUOTE(askapache @ Jul 9 2007, 02:58 PM) *
Web Developers sometimes use file.ext?v=001 as a version control system so they can force visitors to use an updated file. This is so bad.
How is the file.ext?v=001 that is "so bad" really any different from the file-001.ext that you recommend, when both access the same file.ext on the server?
askapache
QUOTE(Darin McGrew @ Jul 9 2007, 07:09 PM) *

QUOTE(askapache @ Jul 9 2007, 02:58 PM) *
Web Developers sometimes use file.ext?v=001 as a version control system so they can force visitors to use an updated file. This is so bad.
How is the file.ext?v=001 that is "so bad" really any different from the file-001.ext that you recommend, when both access the same file.ext on the server?


One reason is because technically when there is a ? like that in the url it represents a query string, so its a hack, not what the specs intended. But the only reason that matters is how browser caches and more specifically non-browser caches deal with it. When a cache sees a URL with a query string in it, that is one of the very strongest things that a cache looks for to determine whether to cache it or not. When a ? is in the url like that most caches DO NOT cache them at all, and that is the major problem and difference. Hey I used to use the foo.js?v=123 hack too and its cool but it really sucks up your bandwidth and slows the whole web down by passing through the major caches..

jake ravenwood
i have a URL www.mydomain.com but my customers are seeing the old index.html file though i already updated it. hitting refresh/F5 in the browser fixes it but i cant tell to all users to refresh to get the page. think they are behind some proxy or they did not clear their cache. how do i make their proxy or browser cache to get a new copy from my server? will the adding ? in the url do? or apache rewriterule do it?

thanks,
jake
askapache
This article uses internal rewrites.. check the headers being sent by your site to see if your htaccess caching is set up correctly.

Adding the cache-control header
CODE
must-revalidate
works well for .html files.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.