How to setup expire headers on Apache?

May 30, 2015 3.4k views
WordPress Caching Ubuntu


I used like 3 different WP plugins but none of them worked so far... so I guess I'll have to do it manually on my server

I haven't found any tutorial though so that's why I'm asking you where and how can I setup expire headers for css, png, jpg,... basically everything (as long as it's useful)

Thanks :)

4 Answers

Try something like this (note: this needs the mod_expires module. Enable it with "a2enmod expires" if you're using Ubuntu or Debian - afterwards restart Apache):
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 30 seconds"
ExpiresByType text/html "access plus 15 days"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType text/js "access plus 1 months"
ExpiresByType text/javascript "access plus 1 months"

I hope it works for you :). Please let me know if it works!

Thank you :)

Before doing anything, can you confirm that I have to put these lines in .htaccess and not in a VirtualHost configuration?

Ok so I tried many times but it still doesn't work:

When I add the mod expires lines I get a 500 error but if I remove it everything works fine...

I did sudo a2enmod expires and sudo a2enmod header and they are both enabled...

Any explanation? I've head it can be something wrong in my httpd.conf but I don't have that file.

  • Hi, why don't you have any file called httpd.conf, or is it a shared server and don't you have access?

    My code should work in Apache 2.2, and I guess in Apache 2.4 too. Do you have access to Apache error log files (usually in /var/log/apache2)?

The issue is resolved (I don't have httpd.conf but apache2.conf!) it just didn't have the right permission...

Thank you!

Have another answer? Share your knowledge.