[solved] rewrite rules access to ENV

#1
This doesn't seem to be working for me (it is always blank). Should it work? Any suggestions?

RewriteCond %{ENV:CACHE_CONTROL} no-cache [NC]
RewriteRule . %{REQUEST_URI}?recache [L]

Logfile always shows:
[REWRITE] Cond: Match '' with pattern 'no-cache', result: -1

Safari dev console shows headers sent:
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control:no-cache
Pragma:no-cache
Referer:http://localhost:3001/poems
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4


ALSO

I was hoping to add cache-control headers via rewrite, but they don't seem to be added to the response headers.
RewriteRule ^/login/js - [L,E=Cache-Control:max-age=0]

But headers still don't show a cache-control setting:
Accept-Ranges:bytes
Content-Encoding:gzip
Content-Length:4730
Content-Type:text/html
Date:Sun, 30 Jan 2011 03:29:52 GMT
ETag:"44a6-4d3dad06-12f451f"
Last-Modified:Mon, 24 Jan 2011 16:47:02 GMT
Server:LiteSpeed
Vary:Accept-Encoding

2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Rule: Match '/login/js/kevin.html' with pattern '^([^.]+)$', result: -1
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Rule: Match '/login/js/kevin.html' with pattern '^(.*)$', result: 2
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Cond: Match '/login/js/kevin.html' with pattern '-adult', result: -1
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Rule: Match '/login/js/kevin.html' with pattern '^/login/js', result: 1
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] add ENV: 'Cache-Control:max-age=0'
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] No substition
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Last Rule, stop!


Thanks!
 
Last edited by a moderator:
#3
Awesome, works great!

Any tips on the 2nd problem, adding a cache-control header? Are the E- headers just read by litespeed, or can they be added to the header sent to the client?

Basically I want to have a custom expiration date for a particular regex of files, where most of them are cached for much longer...

RewriteRule ^/login/js - [L,E=Cache-Control:max-age=0]
 

webizen

Well-Known Member
#4
'Cache-Control:max-age=0' is added as environment variable (can be seen as $_SERVER['Cache-Control']) only as shown below:
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] Rule: Match '/login/js/kevin.html' with pattern '^/login/js', result: 1
2011-01-29 19:29:52.128 INFO [127.0.0.1:50046-0#ap] [REWRITE] add ENV: 'Cache-Control:max-age=0'
 

fantasydreaming

Well-Known Member
#7
It's a static file - I just want to control the expires header information for it on a file-specific level, rather than the default-by-file-type level.
 

NiteWave

Administrator
#8
for static file, I think [E=Cache-Control:max-age] not working at all, since litespeed cache is designed for cache dynamic page(php etc), not for static file.

for static file, there are only 2 apache directives available:
ExpiresByType
ExpiresDefault

so it looks not possible to set a different expiration header for a specific file, in apache and litespeed.
 

fantasydreaming

Well-Known Member
#9
pre-gzipped files

Thanks!

I've got another similar question to run by you guys - I'm trying to render a cached file that's already gzipped, but it is automatically saving the file rather than decoding it in the browser.

#RewriteCond %{HTTP:Accept-Encoding} .*gzip.*
RewriteCond %{DOCUMENT_ROOT}/cached/%{HTTP_HOST}%{REQUEST_URI}.html.gz -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^.]+)$ %{DOCUMENT_ROOT}/cached/%{HTTP_HOST}%{REQUEST_URI}.html.gz [L]

There must be something I'm missing to do with mime types. Any suggestions?
 
#11
I haven't been using any .htaccess, just the litespeed rules. Are there rules allowed in .htaccess (besides rewrite ones) that litespeed understands that might help?

Thanks!
 
#13
Worked great, thanks!

last one I promise! 404 images are still calling my rails process, despite my attempts to stop this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(^/images)
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

The logfile shows it matching the !/images, but still ends up rewriting. Does this need to be a chained rule, or will it match AND on the two conditions by default?

2011-02-10 08:29:55.898 INFO [127.0.0.1:64153-0#ap] [REWRITE] stat( /Users/inspire/Dropbox/newap/public/images/asdf.jpg ) failed
2011-02-10 08:29:55.898 INFO [127.0.0.1:64153-0#ap] [REWRITE] Cond: Match '/images/asdf.jpg' with pattern '(^/images)', result: 2

-> now it seems to restart the whole rewrite process with /dispatch.lsapi
2011-02-10 08:29:55.898 INFO [127.0.0.1:64153-0#ap] [REWRITE] Rule: Match '/dispatch.lsapi' with pattern '^/(.*)', result: 2
2011-02-10 08:29:55.899 INFO [127.0.0.1:64153-0#ap] [REWRITE] stat( /Users/inspire/Dropbox/newap/public//dispatch.lsapi ) failed
2011-02-10 08:29:55.899 INFO [127.0.0.1:64153-0#ap] [REWRITE] Cond: Match '/dispatch.lsapi' with pattern '(^/images)', result: -1
2011-02-10 08:29:55.899 INFO [127.0.0.1:64153-0#ap] [REWRITE] Source URI: '/dispatch.lsapi' => Result URI: 'dispatch.cgi'


This seems to work, but it's inelegant and does another stat call per request.
RewriteCond %{REQUEST_URI} ^/images
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .* / [R=404,L]


Also, just noticed the strange "//" in the test for the dispatch.lsapi file. Strange that it's trying to stat it there, failing, but then still routing to rails.


I'm using the "Rails" context - could it be this is adding some default error handlers or rewrite rules that I don't want? The LSAPI context type seems to only be for php...
 
Last edited:
#15
Yeah I was just looking into the contexts stuff this morning. What an amazingly powerful area! Litespeed is really pushing the limits on powerful and efficient ways of handling complex webmaster requirements.

I was having a bug with the pcre system dropping the first char, so have opened a new thread in the bugs forum.
 
Top