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>
Related
I want to cache all images for 1 month and it works great but the problem is when I try to exclude a subdirectory from caching.
So there are images on:
/ (base dir)
/IMG/
/IMG/folder/
IMG/BIG/
so all images need to be cached, but i want to make it to not cache images that are on IMG/BIG/ folder
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
</IfModule>
the above code works but when
i try this code to exclude /IMG/BIG then it doesn't work
<Directory "/IMG/BIG">
<FilesMatch "\.(jpg|png)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
</FilesMatch>
</Directory>
I want to fix this in the .htaccess that is in the root folder and not by adding another .htaccess inside /IMG/big folder
ExpiresByType image/jpg "access plus 1 month"
Aside: The correct mime-type for JPEG files is image/jpeg, not image/jpg, so the above probably isn't doing anything.
The <Directory> directive is not permitted in .htaccess - this should have resulted in a 500 Internal Server Error (the details of which would be in your server's error log).
The <IfModule> wrappers are not required, unless you intend to use the same config on multiple servers where mod_expires and/or mod_headers are not enabled (unlikely).
To target everything else except the /IMG/BIG subdirectory then you can use an <If> expression and check against the REQUEST_URI server variable with a negated regex (!~ operator). For example:
<If "%{REQUEST_URI} !~ m#^/IMG/BIG/#">
ExpiresActive on
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
</If>
To specifically override any headers for requests to /IMG/BIG/ then you could add an <Else> directive:
<Else>
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</FilesMatch>
</Else>
Unless there are other file types in the /IMG/BIG/ directory that you might want cached then you could remove the <FilesMatch> container.
I've been trying to get this to work for hours now. The browser is downloading the file every time even though the Expires header is set to a week from now. Also tried on Firefox, same result. How can I specify that this resource is still valid and there's no need to download it every time?
Here is the Chrome network log
Here is the Chrome headers for the javascript file.
Here is my .htaccess code. I have confirmed that mod_expire is enabled.
ExpiresActive On
<FilesMatch "\.(css|js|gif|png|jpg|jpeg)$">
ExpiresDefault "access plus 1 week"
Header append Cache-Control "public"
</FilesMatch>
Change httpd.conf
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FilesMatch>
</IfModule>
Set cache to 2 years
Header set Cache-Control "max-age=63072000, public"
I am working on one of the cakephp2 website speed improvement.
now i need to setup some header expire and cache stuff.
but in cakephp in which htaccess I have to put my code.
And please suggest any nice htaccess codes.
I have tried
#Expire Header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 2 hours"
</FilesMatch>
but its not working, also I have tried couple of other code but none of them working for me.Is there any key configuration that am missing?
One more thing if is there any other tricks to improve performance then please suggest me.
add folowing code to .htaccess file
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
Header unset Last-Modified
</FilesMatch>
# cache html/htm/xml/txt diles for 2 days
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
more information http://tutorialpedia.org/tutorials/Apache+enable+file+caching+with+htaccess.html
below following can add it to modify the .htaccess files in your app/webroot/.htacces folder with impunity.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/x-javascript A8640000
ExpiresByType text/javascript A8640000
ExpiresByType text/css A8640000
ExpiresByType image/png A8640000
</IfModule>
or if you can also see in detail at cakephp.org
hope this will sure help you
I hope someone can help me with this as it's driving my crazy.
I have set up mod_deflate on a server, everything SEEMS to be set up correctly, however when I access a CSS or JavaScript file via a browser it's saying the files are not compressed.
However, When I do a curl request, the response headers are saying the file is being compressed.
YES, the module is installed and enabled as it is successfully logging:
When I request the file via a browser (Chrom, FF etc), wget, or any of the online page speed tests, it logs something like
"GET /var/cache/_2c95fa0249c997011610ad7ddd3332a8.css HTTP/1.0" gzip, deflate -/- (-%)
When I request via curl, it provides a log like
"HEAD /var/cache/_2c95fa0249c997011610ad7ddd3332a8.css HTTP/1.0" gzip, deflate 108466/358074 (30%)
Is there a way to check to see if mod_deflate is actually working? Am I missing something in the setup of mod_deflate? Could there be a conflict with the expires directive and mod_deflate (I have removed everything but the mod_deflate and it still didn't work)?
Here are the configurations I have set up:
main conf file
<IfModule mod_deflate.c>
DeflateCompressionLevel 4
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{Accept-Encoding}i %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog logs/deflate_log deflate
</IfModule>
.htaccess file
<FilesMatch "\.(css|js)$">
Allow from all
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:pdf|avi|mov|mp3|mp4|rm)$ no-gzip dont-vary
#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
The file in question:
http://diva.akqire.com/var/cache/_2c95fa0249c997011610ad7ddd3332a8.css
Add the following header:
Header set Content-Encoding x-deflate
Apache servers don't send the Content-Length response header if gzip encoding is used.
Is there a way to check to see if mod_deflate is actually working?
Use a custom log to track deflate usage:
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate
References
Configuring mod_deflate
Mod Deflate - Compress Your Web Pages and Save Bandwidth
How To Save Traffic With Apache2's mod_deflate
In my .htaccess file I have:
<FilesMatch "\.(js|css|png|gif|jpg)$">
<IfModule mod_headers.c>
Header set Cache-Control "public, max-age=31536000"
Header set Expires "A31536000"
</IfModule>
</FilesMatch>
I also have a rewrite for /forum-js/forum.js -> /wp-content/plugins/forum/js/forum.js.php, this is the only file that should match the above regex. For some reason, all other resources matching that regex have headers that include Cache-Control and Expires, but this one does not. Is it because it's a rewritten url?
Is it because it's a rewritten url?
Yup, as per your comment, its real extension is .php and the rules associated with that extension are in effect.
The easiest thing would be to send the headers from within the PHP script:
<? header("Cache-Control: public, max-age=31536000");
header("Expires : pA31536000");
You could also use mod_expires instead:
<IfModule mod_expires.c>
<FilesMatch "\.(js|css|png|gif|jpg)$">
ExpiresDefault A31536000
</FilesMatch>
</IfModule>
It also works with MIME media types instead of file name extensions:
<IfModule mod_expires.c>
ExpiresByType application/javascript A31536000
ExpiresByType text/css A31536000
ExpiresByType image/png A31536000
ExpiresByType image/gif A31536000
ExpiresByType image/jpeg A31536000
</IfModule>