6

How to force the user's web browser to reload CSS files with Apache Server?

I updated my website's CSS file but some users are still seeing problems that would be resolved if their browsers fetched the latest versions.

2 Answers 2

3

This would be for wordpress but you can use some of the php functions to do the same on your server

One way to solve this is to “version” your CSS file, by adding ?v=123 to the URL in the to your stylesheet.

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />

This automatically updates the ?12345678 ending part every time you modify the file. Now everyone instantly sees your changes.

2

In Apache you can control the user's browser cache by adding the following code to an .htaccess file. This file should be in the root of the website or in the folders you want to modify the default

<IfModule mod_mime.c>
    AddType text/css .css
</IfModule>

<IfModule mod_expires.c>
    ExpiresByType text/css                  "access plus 1 hour"
</IfModule>

Please note that in some Apache servers .htaccess may not work, or the server may not have the required modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.