I'm having a problem with my .htaccess file. I'm optimizing my site using Google Pagespeed and GTMetrix and both keep saying "leverage browser caching". So I just added my .htaccess file and included the following lines
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
I first had nothing, then I tried with just the 'expires caching', first piece of code and after that didn't change a thing in the re-tested results, I added the second pair of code -> cache control headers. Strangely, I'm still scoring the same.
I'm on the verge of thinking my HTaccess file looks alright, but there is a problem on perhaps my server.. Or well, I just don't know. You guys probably do!
My .htaccess file is in the same folder as the index.html and assets folders are located. The file structure looks like this:
/public_html/domain/company_name/website/.htaccess - For the .htaccess file /public_html/domain/company_name/website/index.html - For the index.html file
THIS URL will link to the index.html file above.
When writing the title, I saw numbers of other related topics, however, most of them didn't work. Where many related topics posted something about a corrupted or interfering file in the /etc/.. I don't have a /etc/ folder, so that couldn't be it.
Ok, so I found the problem! For anyone who can't find why it ain't working for them, try this!
My problem was that mod_expires wasn't active.
Create a php file and paste in the following code
<?php
print_r(apache_get_modules());
?>
And see if mod_expires is active. If it isn't showing, try this tutorial to active mod_expires. Then edit your htaccess file again and you'll probably see it's working!