Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for some sample code to get me started experimenting with cache control in Apache, with .htacces or httpd.conf.

Can anyone give me any code i can copy and paste in? (and then start tweaking on my own)

lets say i wanted to

cache js,css,images for 30 days

not cache my html as it is fresh every few mins so

not cache anything ending in .php, .html or '/'

I dont want this to look like i'm asking someone to do this for me, - i'm just after some code i can start messing around with!

share|improve this question
up vote 2 down vote accepted

As Konerak said you can use mod_expires, to enable it in a shell do:

# for ubuntu
sudo a2enmod expires
sudo service apache2 restart

# or, for other distros as root:
a2enmod expires
/etc/init.d/apache2 restart
/etc/init.d/httpd restart

here's a vhost configuration that would certainly fit your needs:

<VirtualHost *:80>

    ExpiresActive On
    ExpiresByType image/gif "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/x-icon "access plus 30 days"
    ExpiresByType image/ico "access plus 30 days"
    ExpiresByType text/css "access plus 30 days"
    ExpiresByType application/javascript "access plus 30 days"
    ExpiresByType application/x-javascript "access plus 30 days"
    ExpiresByType text/javascript "access plus 30 days"


Everything else should not be cached.

The script is an extract from my configuration for my website at you can see that it caches only images. (Javascript cache is not enabled.)


share|improve this answer
cheers man, that's very kind of you. I've never done any vhosts stuff (or anything besides mode_rewrite and 301s) in apache.. does that just go in a htaccess file? – Haroldo Jun 20 '10 at 20:07
Yes you can try adding the "Expires" to the .htaccess (do not add the virtualhost tags). If it does not work, you will probably have to add the Expires to your configuration (probably httpd.conf or /etc/apache2/sites-available if using ubuntu) – Weboide Jun 21 '10 at 10:13

use the mod_expires from Apache. You should look into the ExpiresByType.

share|improve this answer
will it detect '/' as type:html? I'm still looking for an actual sample piece of code from someone's site (regardless of whether it does what i want) so i can see a 'complete' cache choice for a certain site – Haroldo Jun 19 '10 at 9:12
Types aren't determined by the requesting URL, they're determined by the document that is returned. So if you index is set to index.html, it'll pickup the extension on that file. – Chris S Jun 21 '10 at 12:54

ExpiresActive On

    # expire All after a month in the client's cache
    ExpiresDefault A2592000

    # HTML documents are good for a week from the time they were changed
    ExpiresByType text/html M604800
    ExpiresByType application/javascript M604800
    ExpiresByType text/javascript M604800
    ExpiresByType text/css M604800

    # Don't cache
        <FilesMatch "\.(php)$">
            ExpiresActive Off

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.