What is mod_expires?
Caching means a browser keeps a saved copy of a file, such as a picture. The next visit is faster because the browser does not download it again.
mod_expires is a part of the Apache web server. It adds a note to each file that says how long the browser may keep it. It is like a "use by" date on food.
Before you start
- Your site must run on Apache. Ask Hostvento support if you are unsure.
- mod_expires must be turned on. On most hosting it is.
- You will edit a file called
.htaccess. It is a settings file in your site folder. Dot files are hidden, so turn on "show hidden files" in your File Manager.
Warning: a mistake in .htaccess can break your site. Download a backup copy first.
Steps
- Log in to your hosting control panel.
- Open File Manager and go to your website folder, often
public_html. - Find
.htaccess. If it does not exist, create it. - Download a copy as a backup.
- Open the file for editing.
- Add the block below at the end of the file.
- Save the file.
- Reload your site and check it works.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
These lines turn on expiry and set a time for each file type. The IfModule wrapper keeps your site safe if the module is missing.
Choosing times
- Pictures and fonts rarely change. A long time such as one year is fine.
- CSS and JavaScript change more often. One month is a safe choice.
- Web pages should have a very short time or none.
Check that it works
- Open your site in a browser.
- Press F12 to open developer tools and click the Network tab.
- Reload the page and click an image.
- Look for an
ExpiresorCache-Controlheader in the details.
You can also run a speed test tool like GTmetrix. It warns you when caching is missing.
Tip: If you change a cached file, visitors may still see the old one. Rename the file or add a version number, such as
style.css?v=2.Quick recap
- mod_expires tells browsers how long to keep files.
- Add rules to
.htaccessafter taking a backup. - Use long times for images and short times for pages.
- Check the headers in your browser tools.