News | February 16, 2015 | No Comments

If you turn on browser caching via .htaccess file, it can increase your website’s page loading speed. But there are times when you do not want this turned on, and would want to have it turned off for subdirectories of your site. I had this problem.

I had a membership portal set up in a subdirectory of my website which ran off of a completely separate Content Management System from the rest of the website. It wasn’t until many months later that I started receiving emails from members stating that the hidden content wouldn’t show up after logging in. For the longest time, I couldn’t figure what the issue was since it was awhile since I turned the browser cache setting on. I assumed it was a bug in the Wishlist software I discussed before.

The problem was because I turned the browser cache setting on in the .htaccess file for the root (main) directory of the server and those settings were taking effect throughout all subdirectories.

The Solution: Turn Off Browser Cache

After remembering the browser cache, I decided to remove the root .htaccess file and noticed the membership didn’t have any further issues. If you would like to keep the browser cache on for most of the website, but turn it off for a subdirectory, just create a separate .htaccess file for that subdirectory with the below code:

<IfModule mod_expires.c>
ExpiresActive off
</IfModule>

<IfModule mod_headers.c>
Header unset Cache-Control
</IfModule>

Turning Browser Cache ON

If you would like to turn on browser caching, you may use a similar code that I used below. Alter the times to your preference.
## EXPIRES CACHING ##

ExpiresActive On
ExpiresByType image/jpg “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/pdf “access plus 1 month”
ExpiresByType text/x-javascript “access plus 1 year”
ExpiresByType application/javascript “access plus 1 year”
ExpiresByType application/x-shockwave-flash “access plus 1 month”
ExpiresByType image/x-icon “access plus 1 year”
ExpiresDefault “access plus 1 month”

## EXPIRES CACHING ##

Leave a Reply