I just setted up a local environament with same code that I had in production environament into a vagrant virtual machine with ubuntu32,
The thing is that I got Internal Server Error with all my rules in the .htaccess file
So I started removing separated blocks of code and this was the trouble:
# 1 weeks
<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 1 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
The thing is that I would like to use the exact same code for all environaments, is there a way I can only include that block of <filesMatch> only if not in localhost?
I found this answer but it just refers to a page, not the host name.
You can probably do:
SetEnvIf Host ^ NON_LOCAL
SetEnvIf Host localhost !NON_LOCAL
<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public" env=NON_LOCAL
</FilesMatch>
# 1 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public" env=NON_LOCAL
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate" env=NON_LOCAL
</FilesMatch>
Related
Is this statement correct?
css and js should use private for proxy caching
https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
I'm intending to use this directly for my hosting and alt. through a CDN, and wish to have the best settings for speed optimization, TTFB.
<IfModule mod_headers.c>
Header set Connection keep-alive
<filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
Header set Cache-Control "max-age=2592000, public"
</filesmatch>
<filesmatch "\.(jpg|jpeg|png)$">
Header set Cache-Control "max-age=1209600, public"
</filesmatch>
# css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
<filesmatch "\.(css)$">
Header set Cache-Control "max-age=31536000, private"
</filesmatch>
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=1209600, private"
</filesmatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</filesMatch>
I have used this before;
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "now plus 2 weeks"
// Lots omitted here
</IfModule>
And this;
<IfModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|JPG)$">
Header set Cache-Control "max-age=1209600"
</filesMatch>
// Lots omitted here
</IfModule>
I can set the expires on by content-type and I can set any header I wish by file extension.
But neither of these seem to let you set any header you want by content-type.
I want to set the cache-control header based on the content-type of the response - note that this is not the same as the file extension. I have "friendly URLs" so there is no file extension to be captured by filesMatch so there is no file extension but the content-type is text/html.
How can I set the cache-control header for specific content-types?
In 2.4, you can append expr= to the Header directive instead of env=. For example:
Header set Cache-Control "max-age=3600" "expr=%{CONTENT_TYPE} == 'text/html'"
In the default (non-early) mode, mod_headers runs as an output filter – so the content type is already set and available by the expression parser.
http://httpd.apache.org/docs/2.4/expr.html
I guess you will need to append or set the Cache-Control header first. Please try the snippet below and do not forget the "no-transform" param.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "now plus 2 weeks"
// Lots omitted here
//This is the magic
<IfModule mod_headers.c>
Header append Cache-Control "public, no-transform"
</IfModule>
</IfModule>
If you want to make the cache content-type you can enter it in this way:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/html "access plus 15 days"
</IfModule>
As my title says, I am trying to cache-control the content on my website but it seems that my content isn't being cached.
This is my code in my .htaccess file:
Header unset Pragma
FileETag None
Header unset ETag
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
Header unset Last-Modified
</FilesMatch>
# cache html/htm/xml/txt diles for 2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
I have tried a lot of tutorials but none of them seems to do the trick.
By the way, other .htaccess codes, like redirecting and stuff, DOES work.
I figured it out, you gotta remove Header unset Last-Modified
Header unset Pragma
FileETag None
Header unset ETag
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
</FilesMatch>
# cache html/htm/xml/txt files for 2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
Even though I've added this in my .htaccess file:
Header add Access-Control-Allow-Origin "*"
And this in my apache config: (Inside a section AND outside)
<Files ~ "\.(gif|jpe?g|png|js|json|xml)$">
Header set Access-Control-Allow-Origin "*"
</Files>
It still does not work!
When I get the DIRECTORY it does give me the wanted header, but as soon as I download a specific file the header is nowhere to be found!
How hard can it be?
try
<FilesMatch "\.(gif|jpe?g|png|js|json|xml)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
I made an .htaccess template; is there anything else that should be added or changed?
# DEFAULTS
ServerSignature Off
AddDefaultCharset UTF-8
DefaultLanguage en-US
SetEnv Europe/Belgrade
SetEnv SERVER_ADMIN chrisp#dejan.com.au
# Rewrites
RewriteEngine On
RewriteBase /
# Redirect to WWW
RewriteCond %{HTTP_HOST} ^serpentineseo.com
RewriteRule (.*) http://www.serpentineseo.com/$1 [R=301,L]
# Cache media files
<filesMatch "\.(gif|jpg|jpeg|png|ico|swf|js)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<FilesMatch "\.(js|css|pdf|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
<FilesMatch "\.(html|htm|txt)$">
Header set Cache-Control "max-age=600"
</FilesMatch>
# DONT CACHE
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
# Deny access to .htaccess
<Files .htaccess>
order allow,deny
deny from all
</Files>
Turn Off ETags [1]
FileETag None
ServerTokens Prod, ServerSignature Off [2]
ServerTokens prod
ServerSignature Off
[1]: http://www.askapache.com/htaccess/apache-speed-etags.html Turn off ETags
[2]: http://www.petefreitag.com/item/419.cfm ServerTokens Prod, ServerSignature Off