2
votes

I've just finished a drupal project and I'm going through optimization phase of the site. I've checked cache blocks and CSS and JS aggregation under performance for better load time . but something I noticed, when I run a page speed test or Yslow everything passes but Leverage browser caching. It seems like the expiry date is not set on all the images and css files. My question is do I have to edit .htaccess file or do I need to place the images and css files that are not cached into a particular folder?

Any help is appreciated, many thanks in advance

2

2 Answers

2
votes

You can configure Apache to set specific expire / cache control headers for your image/css/js files using mod_expires and mod_headers.

Here are few examples (general examples -- analyse and consult the manual before applying to your system):

<FilesMatch "\.(html|htm|js|css)$">
    FileETag None
    <IfModule mod_headers.c>
        Header unset ETag
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </IfModule>
</FilesMatch>

Caching with both mod_expires + mod_headers

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0

# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
    ExpiresDefault A29030400
    Header append Cache-Control "public"
</FilesMatch>

# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
    ExpiresDefault A604800
    Header append Cache-Control "public"
</FilesMatch>

# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js|css)$">
    ExpiresDefault A7200
    Header append Cache-Control "proxy-revalidate"
</FilesMatch>

# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
    ExpiresActive Off
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</FilesMatch>
0
votes

I had the same issue for a long time and I just found the trick!

After looking in .htaccess located on drupal's root repository, there is this condition : <IfModule mod_expires.c> ... </IfModule>

The code located inside is not executed because apache's expires_module is just not enable on my server, check if expires_module is present on list returned by :

sudo apache2ctl -M

If not, just activate it by :

sudo a2enmod expires
sudo a2enmod headers
sudo service apache2 restart