File versioning system + expire headers

Oh, the strange things mod_rewrite does!

File versioning system + expire headers

Postby tomimek » Sun Dec 07, 2008 1:01 pm

I use some simple versioning system for my static files - I add 4 extra characters based on md5 of the file. Not all files can be versioned, so it gives me 2 cases:

/js/versioned_file.a32b.js => /js/versioned_file.js + Header(expire in 1year)
/js/unversioned_file.js => /js/unversioned_file.js (nothing changes, no extra headers)

For versioning I use (in vhost.conf):
RewriteRule ^(.*\.)[0-9a-f]{4}\.(css|jpg|gif|png|js|swf)$ /$1$2 [L]
It work very well.

Then to send extra headers just for versioned files:
<FilesMatch ^(.*\.)[0-9a-f]{4}\.(css|jpg|gif|png|js|swf)$>
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FileMatch>

This solution doesn't work because the rewrite rule changes ([L] makes a internal redirecting) the requested_uri, so the filematch is given the file name without the [0-9a-f]{4} part. I tryed to put filematch above rewriterule, but after the internal redirect (can i turn it off?) the headers are gone anyway.
So i thought I'll use environment variables, like so:

RewriteRule ^(.*\.)[0-9a-f]{4}\.(css|jpg|gif|png|js|swf)$ /$1$2 [L,E=OK:1]
Header set "Expires" "Mon, 28 Jul 2014 23:30:00 GMT" env=OK
Header set "Cache-Control" "max-age=315360000" env=OK

But this doesn't work because after the rewriterule sets OK=1, then it makes a internal redirect and (don't know why) the environment variable OK is gone :(

Any help is VERY MUCH APPRECIATED !!!
(sorry for any language mistakes)
tomimek
 
Posts: 3
Joined: Sun Dec 07, 2008 12:36 pm

Postby richardk » Mon Dec 08, 2008 11:57 am

But this doesn't work because after the rewriterule sets OK=1, then it makes a internal redirect and (don't know why) the environment variable OK is gone

A prefix has been added, try
Code: Select all
env=REDIRECT_OK

instead of
Code: Select all
env=OK
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tomimek » Mon Dec 08, 2008 4:02 pm

I've already tryed that earlier by setting it to E=VERSIONED_FILE:1
Checked Your solution:

Code: Select all
RewriteRule ^(.*\.)[0-9a-f]{4}\.(css|jpg|gif|png|js|swf)$ /$1$2 [L,E=REDIRECT_OK:1]
Header set "Expires" "Mon, 28 Jul 2014 23:30:00 GMT" env=REDIRECT_OK
Header set "Cache-Control" "max-age=315360000" env=REDIRECT_OK


it still doesn't work :(

Thanks for help anyway!
tomimek
 
Posts: 3
Joined: Sun Dec 07, 2008 12:36 pm

Postby richardk » Tue Dec 09, 2008 12:17 pm

Not on the RewriteRule, the prefix is added after that.
Code: Select all
RewriteRule ^(.*\.)[0-9a-f]{4}\.(css|jpg|gif|png|js|swf)$ /$1$2 [L,E=OK:1]
Header set "Expires" "Mon, 28 Jul 2014 23:30:00 GMT" env=REDIRECT_OK
Header set "Cache-Control" "max-age=315360000" env=REDIRECT_OK
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tomimek » Tue Dec 09, 2008 12:46 pm

IT WORKS!!!!!!!!!!!!
THANK YOU VERY,VERY,VERY,VERY,VERY MUCH!!!!

You've saved me a lot of work :)
tomimek
 
Posts: 3
Joined: Sun Dec 07, 2008 12:36 pm

Postby dv8 » Fri Apr 10, 2009 10:21 am

In this thread, is the user tomimek using another apache module to change the cache rule in the header response?

I'm looking to do something similar, if the URL contains URL params then set no-cache to header response.
dv8
 
Posts: 6
Joined: Tue Apr 07, 2009 8:58 am
Location: Virginia

Postby richardk » Sat Apr 11, 2009 11:50 am

is the user tomimek using another apache module to change the cache rule in the header response?

Yes, mod_headers (the Header directive).

I'm looking to do something similar, if the URL contains URL params then set no-cache to header response.

You have to set a variable at the end of your RewriteRule with the E flag
Code: Select all
E=OK:1

then you check for it (Redirect_variablename) at the end of the Header directive
Code: Select all
env=REDIRECT_OK
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dv8 » Wed Apr 15, 2009 6:08 am

I'm finding this one a bit elusive. It either always sets the header or not at all.

My URL may contain a QUERY_STRING of 'action' and will contain a specific file reference of 'media.php'
Code: Select all
/media.php?action=getlistbysite&siteid=AEC


I figured I don't need to eval the action param since one can not exist without the other.

I want to set the E value of NO_CACHE to 1, but not do any rewriting (not necessary). And I thought I had it correct, but it always sets the header.
Code: Select all
RewriteRule ^media\.php$ - [L,E=NO_CACHE:1]
Header set "Pragma" "no-cache" env=NO_CACHE


It seems to me that Header doesn't use env=NO_CACHE as a condition or NO_CACHE is set, but not unset.
dv8
 
Posts: 6
Joined: Tue Apr 07, 2009 8:58 am
Location: Virginia

Postby dv8 » Wed Apr 15, 2009 7:20 am

I figured it out
Code: Select all
RewriteCond "%{QUERY_STRING}"   "^action=([^=]*)"
RewriteRule ^media\.php - [L,E=NO_CACHE:1]
Header set "Pragma" "no-cache" env=NO_CACHE
dv8
 
Posts: 6
Joined: Tue Apr 07, 2009 8:58 am
Location: Virginia

Postby richardk » Wed Apr 15, 2009 9:02 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^media\.php$ - [E=NO_CACHE:1,L]
Header set "Pragma" "no-cache" env=REDIRECT_NO_CACHE


Esle
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*)?action=
RewriteRule ^media\.php$ - [E=NO_CACHE:1,L]

Header set "Pragma" "no-cache" env=NO_CACHE
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 3 guests

cron