When you update your website, it might be required to clear website visitors’ cache to ensure that they are not served old web pages. Here’s how to force browser to clear cache using .htaccess in Apache web server.
How to Force Browser to Clear Cache Using .htaccess
Here are the steps to force browser to clear cache using .htaccess in Apache web server.
When .htaccess sets cache expiry on a user’s browser, it is explicitly instructing the browser NOT to check back with the server, until the cache is expired.
So you need to make some changes at the back end to ensure that the cache validation fails and its gets flushed to retrieve latest data from your server.
1. Change URL/File Version
The easiest way to force browser to clear cache is to simply change the URL of your page and redirect the old page to new page in your .htaccess file.
Redirect individual URLs and files
To redirect individual URL like example.com/old-url to /new-url on same domain
Redirect 301 /old-url /new-url
To redirect individual file like example.com/oldfile.html to /newfile.html on same domain
Redirect 301 /oldfile.html /newfile.html
To redirect individual URL like example.com/old-url to newexample.com/new-url on different domain
Redirect 301 example.com/old-url newexample.com/new-url
To redirect individual file like example.com/oldfile.html to newexample.com/newfile.html on different domain
Redirect 301 example.com/oldfile.html newexample.com/newfile.html
In case you have static files (css, js, images) that need to be refreshed, simply change their file name (e.g by adding a version number).
2. Use Meta Tags
You can also change the meta tags of your HTML pages to force the cache to expire sooner and refresh.
<meta http-equiv="refresh" content="3;URL='http://www.yourdomain.com/'">
The above line will force a refresh after 3 seconds of loading the page, thereby clearing cache.