Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: mod_rewrite Version Control for frequently updated files |
|
Author |
|
htaccesselite
Joined: 20 Nov 2006 Posts: 7 Location: Indianapolis, USA
|
Posted: Mon 09 Jul '07 23:55 Post subject: 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 |
|
Back to top |
|
|
|
|
|
|