“Add Expires headers” is a ySlow component that’s missing from most blogs. It’s overlooked because the rules for these expires headers must be buried deep within the bowels of the .htaccess file. Generally speaking, you want to avoid these errors, so most people avoid the .htaccess file. Spelunking in that file is only problematic when you don’t know what you’re doing, though. If you start adding random shit, you’re likely to have a problem.
So, rule #1: Don’t add shit to the .htaccess file if you don’t know what the shit does.
If you violate rule #1, you’ll end up with a website that looks like this.
Rule #2: plan for incompatibility. I can easily drop in new Apache rules, as my server supports just about all of them. Your server might not. If it doesn’t, and you don’t plan for incompatibility, you’re going to end up with a pretty Internal Server Error.
With two two rules in mind, here’s what you need to add. If you want to know what it means, keep scrolling. Otherwise, just copy it, paste it into your .htaccess file and have a nice day.
<IfModule mod_expires.c>
AddType image/x-icon .ico
ExpiresActive on
ExpiresByType image/jpeg A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css M604800
ExpiresDefault M604800
</IfModule>
And here’s the breakdown.
- <IfModule mod_expires.c> If Apache allows expires headers, then process the rules.
- AddType {mime-type} {file extension} ICOs (favicons) aren’t included in the Apache mime-type by default. This command assigns a mime-type (determined by you) to a specific file extension so other rules can be applied to it.
- ExpiresActive on Turns on the Expires Headers
- ExpiresByType {mime-type} {code}{seconds} Here, we’re letting Apache know how long we want a specific type of file to stay cached on your reader’s computer. Or, more accurately, how long we want the reader’s computer to wait before asking for the file again.
- {mime-type} This portion of the command uses the predetermined mime-types, most of which you don’t get to set yourself. Webmaster Toolkit has a nice list of common mime-types.
- {code} Either A or M.
- A tells the browser to not request the file for {seconds} from when it was last requested.
- M tells the browser to not request the file for {seconds} from when the file was last updated.
- {seconds} Time, in seconds.
- 1 hour = 3,600 seconds
- 1 day = 86,400 seconds
- 1 week = 604,800 seconds
- 1 month = [about] 2,629,743 seconds
- 1 year = 31,556,926 seconds
- ExpiresDefault {code}{seconds} Same as ExpiresByType, except it applies to all files that aren’t specifically addressed.


Pingback: .htaccess 给wordpress 开启 Expires headers | 云飞泉官方博客
i am managing my website,realy need this kind of information,really very good information for me,thx very much for sharing with us!
I was looking for a quick answer on Expires Headers in WordPress and found it here. Thanks!
It`s working fine, thank you very much! I had trouble with Internal Server Errors before, now everything is fine.
Best Andreas
No work for me
I have added the code to .htacces but I’m still getting 500 internal server error.
LoadModule expires_module modules/mod_expires.so is enabled at apache conf.
what can be the problem?
thanks
found a hint in the error log:
“ExpiresActive not allowed here”
I added “AllowOverride Indexes” to conf file for the htaccess directory, but still getting 500 error.
thanks so much!
Thanks for this well-documented information, I am sure it will help others to understand more on how to optimize their site.
This code will work for some websites and not others. Luckily it works for mine. You’ll have to google for different types of Add Expires Headers and find a code that works.
very nice tutorial! Thank you