πŸ“„
Performance

Setting Up Browser Caching via .htaccess

πŸ“… 14 October 2026 ✏️ 14 October 2026 ⏱ 5 min leestijd

Learn how to set up browser caching via .htaccess on a LiteSpeed server, including explanations, sample code and common pitfalls.

Browser caching ensures that returning visitors don’t have to download static files like images, CSS and JavaScript again and again. You arrange this through a .htaccess file using the mod_expires and mod_headers modules. In this article you’ll find out exactly which code to add, how to test it, and what to watch out for on a LiteSpeed server.

What is browser caching and why set it up

When a visitor opens your website, the browser downloads all kinds of files: logos, stylesheets, scripts and fonts. Without caching instructions, the browser retrieves these files again on every visit, even if nothing has changed. With browser caching, you tell the browser how long it may store a file locally before requesting it again. This saves on loading time and server load, and noticeably improves the experience for returning visitors.

On a LiteSpeed server, .htaccess is read just like on Apache, so the rules below work without extra configuration. Keep in mind, however, that this type of caching is separate from server-side caching such as LiteSpeed Cache; browser caching only controls what is stored locally on the visitor’s device.

Visitor.htaccess ruleFilefrom cache

The right code for your .htaccess

The most commonly used way to set up browser caching is via mod_expires. Place the following code at the top or bottom of your .htaccess file, outside any CMS-specific blocks:

<IfModule mod_expires.c>nExpiresActive OnnExpiresByType image/jpg "access plus 1 month"nExpiresByType image/jpeg "access plus 1 month"nExpiresByType image/png "access plus 1 month"nExpiresByType image/webp "access plus 1 month"nExpiresByType image/svg+xml "access plus 1 month"nExpiresByType text/css "access plus 1 week"nExpiresByType application/javascript "access plus 1 week"nExpiresByType application/pdf "access plus 1 month"nExpiresByType font/woff2 "access plus 1 year"nExpiresDefault "access plus 2 days"n</IfModule>

If you want extra control, also add caching headers with mod_headers:

<IfModule mod_headers.c>n<FilesMatch ".(css|js|png|jpg|jpeg|webp|svg|woff2)$">nHeader set Cache-Control "max-age=604800, public"n</FilesMatch>n</IfModule>

Adjust the durations based on how often you change your files. Fonts rarely change and can therefore be cached longer than CSS or JavaScript, which you might update regularly.

πŸ’‘ Tip: use version numbers or a query string (such as style.css?v=2) in file names when caching for a long time. This forces an update without having to adjust the caching setting.

Step-by-step setup

1

Back up your .htaccess

Download the current file via the file manager or FTP before making changes, so you can restore it if problems occur.

2

Open the .htaccess file

In Plesk you can find this under your domain’s file manager. Make sure hidden files are visible, since .htaccess starts with a dot.

3

Add the caching code

Paste the ExpiresByType rules and, if applicable, the Headers rules at the bottom of the file, without removing existing rules.

4

Save and visit your website

Check that the site still loads normally. If you get an error (such as a 500 error), remove the added rules and check the syntax.

5

Test the caching headers

Use your browser’s developer tools (Network tab) to check whether files receive the correct Cache-Control or Expires header.

Testing and common mistakes

A common mistake is adding duplicate <IfModule> blocks, which causes settings to conflict with each other. Also check that you’re not overwriting rules already added by your CMS (such as WordPress). If you use a caching or performance plugin, check whether it already adds its own rules to .htaccess to avoid conflicts.

Note that caching settings only affect static files; dynamic pages are not influenced by this. Browser caching has no effect on email-related headers or settings β€” that is configured separately in your business email configuration.

If you’re unsure whether the change has been applied correctly, check the knowledge base for more background articles on server configuration, or get in touch if you can’t figure it out.

Frequently Asked Questions

Does browser caching via .htaccess also work on LiteSpeed servers?

Yes, LiteSpeed is compatible with Apache directives such as mod_expires and mod_headers, so the code works without modifications.

What is the difference between browser caching and server-side caching?

Browser caching stores files locally on the visitor’s device, while server-side caching (such as LiteSpeed Cache) stores generated pages on the server itself so they can be served faster.

My website shows a 500 error after adding the code, what now?

Remove the added rules and check for typos, such as missing quotation marks or an incorrectly closed IfModule tag.

Do I need to set up caching for every file type separately?

That’s not mandatory, but it does give you control over how long specific files are cached, which is useful for CSS or JS that changes regularly.

Does browser caching affect my domain settings?

No, this has no effect on DNS or domain names; it’s purely a setting within your website files.

Conclusion

Setting up browser caching via .htaccess is a simple, technical adjustment that directly affects loading time for returning visitors. With the right ExpiresByType and Cache-Control rules, you decide how long files are stored locally. If your current hosting isn’t cooperating or you lack quick support for this kind of adjustment, take a look at the options for switching to Tandata.

View web hosting β†’

Was dit artikel nuttig?