Set cache headers with .htaccess

Have some amazing content on your website? Since you're here, you probably know what caching is (If you don’t, it’s the fact that your computer saves copies of webpages, so you can view them later without having to re-download copies from the internet). The bad thing about caching is that people might not be able to see your amazing updates until their caching period expires, and that might be awhile! .htaccess can help you control how long your visitor's browser stores your site, so they can see your updates when they are published.

The code

All you have to do is add this code into your .htaccess file and save it! Like all file changes, it can take some time before it updates everywhere, but you can speed up the process on your devices simply by clearing your browser’s cache. This code snippet will control your visitors' caching! The recomended settings have already been added in below, but feel free to change the numbers around to suit your needs. To add more file types, just add the MIME type, then the access command.

.htaccess Example:

<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access plus 7 days"
ExpiresByType text/css "access plus 7 days"
ExpiresByType text/php "access plus 7 days"
ExpiresByType text/js "access plus 14 days"
ExpiresByType text/javascript "access plus 14 days"
ExpiresByType application/javascript "access plus 14 days" 
ExpiresByType image/gif "access plus 12 months"
ExpiresByType image/jpeg "access plus 12 months"
ExpiresByType image/png "access plus 12 months"
ExpiresByType image/webp "access plus 12 months"
ExpiresByType image/x-icon "access plus 12 months"
ExpiresByType application/x-icon "access plus 12months"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
</IfModule>

Note: An .htaccess file located in a sub-directory overrides any duplicate rules from previous .htaccess files. For example, if you have a .htaccess file located in the root defining a 404 and 403 error page, and another .htaccess located in the “test” folder defining only a 404 error page, any files and folders in the “test” folder will use the 404 page defined in the "test" .htaccess file, and the 403 page defined in the root .htaccess file.

Advertisement

Installation

Unsure of how to install this code? It's pretty simple. A .htaccess file is called “.htaccess”, with nothing before the dot. Try finding this on your FTP software. Many systems don’t allow filenames starting with a dot (So downloading a htaccess file can be difficult at times), so that also restricts how we can create a .htaccess file. It is possible to get around these restrictions though, so here are the steps!

  1. Open your text-editing program and code your .htaccess file
  2. Once you are finished, save it as “htaccess.txt” (We will fix its name later)
  3. Upload “htaccess.txt” to your webserver and rename it to “.htaccess” (Remember that the dot is important!)

If you are still confused, or it's not working for you, check out the more detailed instruction in our Finding, Creating, and Editing a .htaccess file article.

Conclusion

Hopefully you were successful in installing this snippet, if you need help, feel free to send us a message! Check out the rest of the htaccess snippets to see what other amazing things you can do!

Advertisement

Article Author

Related Articles

Deny Access to Hidden Files with .htaccess

Is your website ready to go live? It’s always a good idea to block people from accessing files they shouldn't, like your .htaccess file for example! This code will block access to any files beginning ...

Deny Access to a Directory or File with .htaccess

Have your website all setup? Amazing! There might be a directory that you want no one to ever visit, not even yourself! Although this does not really make sense on a web server, here are the instructi...

Finding, Creating, and Editing .htaccess files

.htaccess is amazing! If you haven’t read the <a href="/webhosting/htaccess/what-is-htaccess">introduction to .htaccess</a>, check that out first (Unless you already know the amazing things .htaccess c...

Advertisement

All code and content © 2024. Contact Us to learn more.