htaccess / mod_expires - caching specific files - apache

Ok, I checked a lot of websites about how to manage the browser's cache memory with a .htaccess file, but it is still very not clear to me.
I want to cache specific files for one month. For the rest, I want it to be refreshed every time. So I tried:
<IfModule mod_headers.c>
Header unset Cookie
Header unset Set-Cookie
Header unset Cache-Control
Header unset ETag
FileETag none
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "now"
<Files "/css/jquery-ui.css">
ExpiresDefault "access plus 1 month"
</Files>
<Files "/js/jquery-1.10.2.min.js">
ExpiresDefault "access plus 1 month"
</Files>
<Files "/js/jquery-ui.js">
ExpiresDefault "access plus 1 month"
</Files>
<Files "/js/analytics.js">
ExpiresDefault "access plus 1 month"
</Files>
<Files "/matheos/img/*">
ExpiresDefault "access plus 1 month"
</Files>
<Files "/img/*">
ExpiresDefault "access plus 1 month"
</Files>
</IfModule>
But it doesn't work exactly as expected...
The HTML is correctly not cached, but the specific files like jquery-ui.css, which should be cached for 1 month, are also not cached.
Anyway, does this .htaccess seem ok for you ?

Ok, got it ! To target a specific file, the correct syntax is :
# to not cache css except jquery-ui.css
ExpiresByType text/css "now"
<FilesMatch "jquery-ui\\.css$">
ExpiresByType text/css "access plus 1 month"
</FilesMatch>
This is the only way that worked for me, at least in the case of an ovh shared host. I also tried all possible combinations with ExpiresDefault but it didn't work...

Related

htaccess: cache all images but exclude caching images from subfolder

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.

'403 Forbidden error' when trying to access images on a Wordpress install

I have a dedicated server. I have installed wordpress in a subdirectory on it and uploaded some images via the 'media' option in the dashboard. But I am unable to access images directly (hotlinks) in the browser, however, I can see the images in the file manager (FTP) under /wp-content/uploads folder.
It is a fresh install with no plugins and only the default theme. I haven't done anything other than upload a few images via the media option. Please note that the error shows even when I click the 'library' option under the 'media' option in the dashboard.
I even tried creating a .user.ini file in both the directories with the exact same code as of php.ini as shown below, but that didn't help either. The hosting provider says they don't provide support for dedicated servers. What do I do now?
Here is the root directory .htaccess
<FilesMatch "\.(jpg|png|gif|js|css|ico)$">
<IfModule mod_headers.c>
Header set ETag ""
Header set Cache-Control "max-age=2592000"
Header set Expires "01 Jan 2112 00:00:00 GMT"
</IfModule>
</FilesMatch>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
<FilesMatch "\.(jpg|png|gif|js|css|ico)$">
<IfModule mod_headers.c>
Header set ETag ""
Header set Cache-Control "max-age=2592000"
Header set Expires "01 Jan 2112 00:00:00 GMT"
</IfModule>
</FilesMatch>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
# BEGIN SUGARCRM RESTRICTIONS
RedirectMatch 403 (?i).*\.log$
RedirectMatch 403 (?i)/+not_imported_.*\.txt
RedirectMatch 403 (?i)/+(soap|cache|xtemplate|data|examples|include|log4php|metadata|modules)/+.*\.(php|tpl)
RedirectMatch 403 (?i)/+emailmandelivery\.php
RedirectMatch 403 (?i)/+upload
RedirectMatch 403 (?i)/+custom/+blowfish
RedirectMatch 403 (?i)/+cache/+diagnostic
RedirectMatch 403 (?i)/+files\.md5$
# END SUGARCRM RESTRICTIONS
<FilesMatch "\.(jpg|png|gif|js|css|ico)$">
<IfModule mod_headers.c>
Header set ETag ""
Header set Cache-Control "max-age=2592000"
Header set Expires "01 Jan 2112 00:00:00 GMT"
</IfModule>
</FilesMatch>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
Here is the Wordpress directory .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /laksh/3/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /laksh/3/wordpress/index.php [L]
</IfModule>
# END WordPress
Here is the php.ini code in both the directories:
file_uploads = On
upload_max_filesize = 100M
post_max_size = 100M
The uploaded images won't even display in pages or posts.
Worth checking if
RedirectMatch 403 (?i)/+upload
is the cause of your images not being displayed, the wordpress path for uploaded images, containers the string upload

Keep-Alive doesn't allow all persistent connections

I want to speed up my website. I runned test on http://www.webpagetest.org/ website that checks performance and optimization of website.
I enabled Keep-Alive inside .htaccess file. Problem is that result of testing says that only 18% of all connections are allowed (that ones that are linked or included via google)
ScreenShot:
Can someone explain to me how to allow all connection?
CODE THAT I USED:
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year”
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month”
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
<IFModule mod_deflate.c>
<filesmatch "\.(js|css|html|jpg|png|php)$">
SetOutputFilter DEFLATE
</filesmatch>
</IFModule>
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
Enable KeepAlive configuration in your Apache Configuration file
KeepAlive On

htaccess file being ignored, trying to leverage browser caching for Pagespeed

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!

how to add header expire in cakephp 2

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