Add expiration headers using Apache for paths not on the file system

For CDN invalidation purposes, I need to add a prefix to the path element of the site URL. This changes whenever a new version of an asset is released.

The URL is then rewritten using mod_rewrite from: http://example.com/cdn/20111030/images/image.jpg to http://example.com/images/image.jpg where the actual asset is located.

I would like to add to the answer long expiration headers (at least 3 months) (for the first URL that does not actually exist on the file system). Does anyone know how to do this?

+6
source share
3 answers

From http://drupal.org/node/974350#comment-5305368
These rules are designed for 480 weeks, but you can adjust the time accordingly.

<IfModule mod_rewrite.c> RewriteEngine on <IfModule mod_headers.c> # Transform /cdn/***/ to / RewriteCond %{REQUEST_URI} ^/cdn/([0-9a-zA-Z])*/(.+)$ RewriteRule .* /%2 [L,E=CDN:1] # Apache will change CDN to REDIRECT_CDN. # Set a far future Cache-Control header (480 weeks), which prevents # intermediate caches from transforming the data and allows any # intermediate cache to cache it, since it marked as a public resource. Header set Cache-Control "max-age=290304000, no-transform, public" env=REDIRECT_CDN # Set a far future Expires header. The maximum UNIX timestamp is somewhere # in 2038. Set it to a date in 2037, just to be safe. Header set Expires "Tue, 20 Jan 2037 04:20:42 GMT" env=REDIRECT_CDN # Pretend the file was last modified a long time ago in the past, this will # prevent browsers that don't support Cache-Control nor Expires headers to # still request a new version too soon (these browsers calculate a # heuristic to determine when to request a new version, based on the last # time the resource has been modified). # Also see http://code.google.com/speed/page-speed/docs/caching.html. Header set Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT" env=REDIRECT_CDN # Do not use etags for cache validation. Header unset ETag env=REDIRECT_CDN </IfModule> </IfModule> 

Also see AdvAgg rules for these descriptor servers on which mod_headers or mod_expires are not installed. It uses the FilesMatch directive; Advagg files have a fairly unique file name, so I can do this. AdvAgg redundancy will not work in this case, because mod_expires cannot use environment variables; cannot FileETag . From what I see, mod_headers is the desired way to set far future times in apache.

+4
source

It seems that if you add RewriteEngine / Rule to the Apache configuration for your own solution, the location will be correctly selected and will serve Expires / Cache-Control on / cdn calls and will not serve them for non-cdn calls with a slight change:

  # in apache config
     Rewriteengine on
     RewriteRule ^ / cdn /[^/.BIZ*/(.*) / $ 1 [L]

     <Location "/ cdn">
       Header unset ETag
       FileETag None
       ExpiresActive on
       ExpiresDefault "access plus 1 year"
     </Location>

I see no reason why this should be a problem in the Apache configuration.

+5
source

The solution could be to use Expires for all assets, use mod_headers to remove headers from the non-cdn version, for example:

  Rewriteengine on
  RewriteRule ^ cdn / ([0-9a-z]) * / (. *) / $ 2 [L, E = cdn: 1]

  ExpiresActive on
  ExpiresDefault "access plus 1 year"
  Header unset Expires env =! Cdn
  Header unset Cache-Control env =! Cdn

This is a bit redundant for the root of the website, but if it applied only to assets, that would be a problem.

0
source

Source: https://habr.com/ru/post/900392/


All Articles