View Single Post
  #1 (permalink)  
Old 07-09-2007, 06:51 PM
modrewrite modrewrite is offline
WebProWorld Member
 
Join Date: Dec 2006
Location: Indianapolis
Posts: 41
modrewrite RepRank 0
Lightbulb mod_rewrite Version Control for frequently updated files

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
__________________
de // AskApache.com blog
Reply With Quote