Cache Control in htaccess file causing internal server error [duplicate] - .htaccess

In my .htaccess I have this code:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|txt|html|x-html|php|css|xml|js|woff|ttf|svg|eot)(\.gz)?$">
ExpiresActive On
Header set Expires "Sun, 27 May 2012 00:00:00 GMT"
Header unset ETag
FileETag None
</FilesMatch>
It seems to work perfectly on some servers, but not on one of my websites. I get a 500 Internal Server Error.
Is there anything wrong in the config, or do I have to contact my host?

Ensure that you have these Apache modules enabled and loaded:
ExpiresActive -- mod_expires
Header -- mod_headers
Try this instead (it will only use directives if corresponding module is present):
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|txt|html|x-html|php|css|xml|js|woff|ttf|svg|eot)(\.gz)?$">
<IfModule mod_expires.c>
ExpiresActive On
</IfModule>
<IfModule mod_headers.c>
Header set Expires "Sun, 27 May 2012 00:00:00 GMT"
Header unset ETag
</IfModule>
FileETag None
</FilesMatch>

Related

Static CSS/JS files with Expires and Cache-Control headers are not cached in browser

I have setup browser caching of all images and css/js files. Still, when I look a the browser's web developer tools under the network panel, it seems like each cached file is nonetheless retrieved from the server.
This is what I have in my .htaccess:
<ifModule mod_headers.c>
<ifModule mod_expires.c>
# images for 1 hour
<filesMatch "\.(jpeg|jpg|png|gif|flv|pdf|swf)$">
ExpiresActive On
ExpiresDefault "access plus 1 hours"
Header set Cache-Control "public, max-age=3600, must-revalidate"
</filesMatch>
# CSS and js for longer
<filesMatch "\.(ico|css|js|eot|ttf|ttc|otf|woff)$">
ExpiresActive On
ExpiresDefault "access plus 7 days"
Header set Cache-Control "public, max-age=604800, must-revalidate"
</filesMatch>
</ifModule>
</ifModule>
# do not use ETag
FileETag None
For an CSS file like https://www.fiveroasters.de/wp-includes/css/dashicons.min.css?ver=3.9.2 this results in the following headers:
Accept-Ranges bytes, bytes
Age 0
Cache-Control public, max-age=604800, must-revalidate
Connection keep-alive
Content-Encoding gzip
Content-Length 24803
Content-Type text/css
Date Fri, 26 Sep 2014 21:26:10 GMT
Expires Fri, 03 Oct 2014 21:26:10 GMT
Last-Modified Tue, 27 May 2014 09:24:09 GMT
Server Apache/2.4.10
Vary Accept-Encoding
Via 1.1 varnish
This looks right to me, still the browser does not cache it, but each request still causes a full download (http code 200). Same for JS, images, etc. Why? You can have a look yourself at https://www.fiveroasters.de
(I have the cache settings enabled under the webtools settings.)

Exceptions to Filesmatch htaccess rule?

Gidday
I've currently got js files set up as must-revalidate, to overcome mobile networks caching old versions when I do updates.
<FilesMatch ".(js)$">
Header set Cache-Control "max-age=608000"
Header set Cache-Control "must-revalidate"
</FilesMatch>
I have some .js files that I never change, so I was wondering how to go about making an exception for these files?
Thanks for your time and help.
You can add another FilesMatch section for those specific files:
<FilesMatch "\.js$">
Header set Cache-Control "max-age=608000"
Header set Cache-Control "must-revalidate"
</FilesMatch>
# CACHED FOREVER
<FilesMatch "(file1|file2)\.js$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>

htaccess: content doesn't seem to get cache'd

This is my code for cacheing my website in the user's browsers:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
Header unset Pragma
FileETag None
Header unset ETag
# cache images/pdf docs for 30 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js|ttf|woff|eot|svg)$">
Header set Cache-Control "max-age=2592000, public, must-revalidate"
Header unset Last-Modified
</FilesMatch>
# cache html/htm/xml/txt diles for 10 days
<FilesMatch "\.(html|htm|xml|txt|xsl|css|php)$">
Header set Cache-Control "max-age=864000, must-revalidate"
</FilesMatch>
</IfModule>
## EXPIRES CACHING ##
When I check my website's performance on http://developers.google.com/speed/pagespeed/insights/ it doesn't seem like it's getting cache'd.
Is there anything wrong with the code? Or could it have something to do with a deeper server setting?
Thanks!
I solved the issue by installing mod_headers and mod_expires on the server. Quite useful if you're going to use their functions...

Do I have to specify cache headers for each page individually?

This is my code to specify cache expirations:
# Expires
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header unset Last-Modified
Header set Cache-Control "public"
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi|html?)$">
Header set Cache-Control "private, must-revalidate, proxy-revalidate"
ExpiresDefault A0
ExpiresActive Off
</FilesMatch>
-I'm running APACHE on mediatemple. - Do I have to include an .htaccess file with this code on every directory or is there a way to make it work for the whole site?

.htaccess - Cache files from URL like /files/large/6

We have a multisite CMS that handles images and other files like this..
How can we cache images and other files that are in www.(or non-www.)variable-domain.com/files/* with .htaccess?
This is causing a 500 error. I stripped out some.. here is what I have currently that works (minus the Directory and contents part - it throws the error when thats included).
#
# Force Browser Cache
#
<ifmodule mod_expires.c>
ExpiresActive On
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresDefault "access plus 1 year"
</filesmatch>
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
<FilesMatch ".(js|css|pdf|txt)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
<Directory "/home/aiwebsystems/public_html/uploads">
<FilesMatch "\.(gif|jpg|png|js|css)$">
ExpiresDefault "access plus 1 year"
</FilesMatch>
</Directory>
</ifmodule>
I would need all subdirectories of this included too...
Thanks for the help!
Use Apache's mod_expires
e.g in your .htaccess put:
ExpiresActive On
<Directory "/path/to/public_html/files">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
ExpiresDefault A300
<FilesMatch "\.html$">
Expires A86400
</FilesMatch>
<FilesMatch "\.(gif|jpg|png|js|css)$">
Expires A2592000
</FilesMatch>
</Directory>
A300 meaning that the user cached copy expires 300 seconds after access. (A86400 is a day after access, A2592000 is a month after access)
If you mean server side caching, well then you are in luck as the operating system caches recently using a 'paging' algorithm: http://en.wikipedia.org/wiki/Paging
<Directory> is not allowed in htaccess.
Just create a new .htaccess file, with the expires stuff, and put it inside the 'uploads' directory. This will have the same effect you try to achief
Being as it does not have an extension, none of this worked. I change the code in the end and it works great now, using the file name rather than the image ID as the last URI parameter.

Resources