Search code examples
phpapache.htaccesscachingmod-expires

PHP configuration overriding the .htaccess caching headers


I have been trying setting up caching for my site's static files like jpg, png, css etc. but getting no success so far, I figured out that PHP's configuration is taking over the .htaccess is config.

I have verified that mod_expires.so is uncommented in my httpd.conf.

I tried following in .htaccess but in vain.

<IfModule mod_expires.c>
ExpiresActive On
<FilesMatch "\.(jpe?g|png|gif|js|css|ttf|eto|svg|woff|otf)$">
ExpiresDefault "access plus 1 month"
</FilesMatch>
</IfModule>

even tried without success

<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

I then found that my php.ini has session.cache_limiter = nocache I changed it to session.cache_limiter = public and my session.cache_expire=180 after changing this config in php.ini and restarting apache, I found that response header for images changed as below

HTTP/1.1 200 OK
Date: Fri, 19 Sep 2014 07:33:58 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.2.10
Expires: Fri, 19 Sep 2014 10:33:58 GMT
Cache-Control: public, max-age=10800
Last-Modified: Mon, 05 Dec 2011 19:08:22 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

What I want to do is to cache the images and css/js files for "access plus 1 month"

any suggestion is appreciated.


Solution

  • You can use ExpiresByType directive:

    ExpiresActive on
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"