[solved] rewrite rules access to ENV

Discussion in 'General' started by fantasydreaming, Jan 30, 2011.

  1. fantasydreaming

    fantasydreaming Well-Known Member

    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:
    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


    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:
    Date:Sun, 30 Jan 2011 03:29:52 GMT
    Last-Modified:Mon, 24 Jan 2011 16:47:02 GMT

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

    Last edited by a moderator: Feb 11, 2011
  2. NiteWave

    NiteWave Administrator

    please try:

    RewriteCond %{HTTP:Cache-Control} no-cache [NC]
    RewriteRule (.*) $1?recache [L]
  3. fantasydreaming

    fantasydreaming Well-Known Member

    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]
  4. webizen

    webizen Well-Known Member

    'Cache-Control:max-age=0' is added as environment variable (can be seen as $_SERVER['Cache-Control']) only as shown below:
  5. fantasydreaming

    fantasydreaming Well-Known Member

    Any way to add it as a header instead?
  6. mistwang

    mistwang LiteSpeed Staff

    You should update the script to add a response header.
  7. fantasydreaming

    fantasydreaming Well-Known Member

    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.
  8. NiteWave

    NiteWave Administrator

    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:

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

    fantasydreaming Well-Known Member

    pre-gzipped files


    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?
  10. NiteWave

    NiteWave Administrator

    what's the content of DOCUMENT_ROOT/cached/.htaccess ?
  11. fantasydreaming

    fantasydreaming Well-Known Member

    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?

  12. NiteWave

    NiteWave Administrator

    here's example FYR:

    when you access http://domain.com/cached/hello
    will return content of $DOCUMENT_ROOT/cached/hello.html.gz
    and display in browser correctly.
  13. fantasydreaming

    fantasydreaming Well-Known Member

    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 [] [REWRITE] stat( /Users/inspire/Dropbox/newap/public/images/asdf.jpg ) failed
    2011-02-10 08:29:55.898 INFO [] [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 [] [REWRITE] Rule: Match '/dispatch.lsapi' with pattern '^/(.*)', result: 2
    2011-02-10 08:29:55.899 INFO [] [REWRITE] stat( /Users/inspire/Dropbox/newap/public//dispatch.lsapi ) failed
    2011-02-10 08:29:55.899 INFO [] [REWRITE] Cond: Match '/dispatch.lsapi' with pattern '(^/images)', result: -1
    2011-02-10 08:29:55.899 INFO [] [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: Feb 10, 2011
  14. mistwang

    mistwang LiteSpeed Staff

    just add "static" context for "/images" could be an easy fix. no rewrite rules involved.
  15. fantasydreaming

    fantasydreaming Well-Known Member

    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.

Share This Page