apache ExpiresDefault - just doesn't work - apache

we all know that setting expire date for static files is very useful and the way i found to do so is through the ExpiresDefault property in a .htaccess file but it just doesn't work. I am using YSlow and page_speed to test the HTTP response, but it just keep on telling me that I need to set an expire date for my JS, CSS and images to speed up my application.
I tried to test ExpiresDefault "access plus 1 year" , locally ( apache server ) and on the web ( hostgator reseller account. if I have any options to make use of !?! ) but it just doesn't work. And it also doesn't return 500 error.
I feel like I am missing the main point or doing it wrong. How the whole thing should be done ?

First, identified mod_expires is compiled inside your apache.
Try
PATH_TO_YOUR/httpd -M | sort /* <-- look for expires_module*/
And your apache configuration should be like
ExpiresActive on
ExpiresDefault "access plus 1 year"
And always restart apache after configuration changed

Related

.htaccess ExpiresDefault 1 day

If I add only these 2 lines to the .htacces file, will all the files on the website be kept in the cache for 1 day?
ExpiresActive On
ExpiresDefault "access plus 1 day"
Is it enough and it works?
ExpiresActive On
ExpiresDefault "access plus 1 day"
If you have no other overriding directives then these directives do indeed instruct the browser to cache the response for 1 day, by sending back the appropriate Expires and Cache-Control: max-age HTTP response headers. (Expires is required for old browsers. All modern browsers will use the Cache-Control header instead.)
Is it enough and it works?
Whether it is "enough" is entirely dependent on the nature of your site. Depending on your site, some responses perhaps shouldn't be cached at all, whilst other static resources should be cached for much longer.
And it's entirely possible you have other directives or even a front-end proxy or CDN that overrides these response headers.

Apache: set max-age or expires in .htaccess for directory

I have a handful of directories with content which will never change.
Is it possible to create .htaccess file which tells the browser that anything in this directory and sub- directories can be cached for a very long time?
I would like to copy the same .htaccess file in each directory if possible.
If this is possible would you recommend max-age over expires or both?
So it does look possible.... the .htaccess file syntax is:
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
This will turn off Etags and turn on cache-control: max-age
Then put this .htaccess file in the directory and all files (including it's sub-directories will be cached for 1 year.
I decided to put all my cache-able content under a single root directory and edit the httpd.conf as
<Directory "C:\somedir\cache">
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
</Directory>
I am still in the process of testing this. I just hope this does not turn off Etags for the rest of the site. So far it looks like it's working as planned.
UPDATE (after 6 months):
Setting the ExpiresDefault and allowing e-tags is the best thing to do.
in httpd.conf:
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Directory>
Make sure "somedir" is inside of the apache root (such as htdocs).
Allowing e-tags is a good because after 1 year, the browser will re-validate the file by passing the e-tag. The web server will send back a 304 - Not Modified and reset the max-age to 1 year. This is very efficient.
All in all, you can watch the apache log file and see that items in /cache dir are begin served once.
Note: I have found that setting Header append Cache-Control "public" is ok to do if you want.
Final Version:
Here's the final version: (just add this at the bottom of the httd.conf)
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</Directory>
Inspecting the header should reveal this:
Accept-Ranges:bytes
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Language:en
Content-Length:746
Content-Type:text/css
Date:Thu, 29 May 2014 15:23:50 GMT
ETag:"240000000add63-2ea-4f4086d72ad01"
Expires:Fri, 29 May 2015 15:23:50 GMT
Keep-Alive:timeout=40, max=200
Last-Modified:Fri, 07 Mar 2014 18:28:59 GMT
This will:
Set the max-age for 1 year (the longest recommended)
Send the expires tag of 1 year
Send an Etag, so after 1 year the browser will perform etag validation
Let intermediate caching devices/services know that they can cache the file for 1 year.
FYI, if you do what is mentioned above and your Apache won't restart then you may be getting this error:
The Apache service named reported the following error:
>>> Invalid command 'ExpiresActive', perhaps misspelled or defined by a module not included in the server configuration.
You can find that error by clicking "Start", type in "Computer Management", launch it, in the tree open "Event Viewer -> Windows Logs -> Application". That's where I found the error above.
Easy fix, just uncomment this line in httpd.conf:
#LoadModule expires_module modules/mod_expires.so

Does Mod Deflate automatically cache the defalted files?

I am using mod_deflate to compress javascript files. I want to know if the deflated files are cached somewhere or the files are compressed everytime they are requested for.
If they are cached, is there a way to configure the cache to improve the performance or tweak the settings ?
Thanks
NO,
you can tell the browsers to cache it on there end aswell my using
a2enmod expires
and to configure mod expire you need to do this inside this file
/etc/apache2/sites-available/default
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 week"
</FilesMatch>
</IfModule>
test in the browser.
Now if you want to use more advanced caching they why not use varnish. If you have static content varnish will make you website fly and less burden on your apache.

RewriteRule & Header set Expires : how to

I'm using rewrite rules to create a /fr /en on my website and does folder don't exist, therefore if I try to use
<Directory /fr>
ExpiresDefault "access plus 1 day"
</Directory>
Apache complain because the folder does not exist, can't find a way to do it in the http.conf
If I use
Header set Expires "access plus 1 day"
in the .htaccess, can see the header showing
Expires: access plus 1 day
instead of the date + 1 day, if i remove it I can see
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Also if I use
ExpiresDefault "access plus 1 day"
It does not work...
Can you please tell how to get this right?
Also I believe that Safari (and only safari) keep on reloading the page every minutes because of that, is that correct or just another issue?
Thx for your help!
Directory instructions are working on real filesystem paths. So a Directory setting should looks like :
<Directory /var/www/foo/bar/fr >
(...)
</Directory>
Instead, if you prefer working with url path you must use Location directives:
<Location /fr >
(...)
</Location>
This should at least fix your inexistents directories problems (if I understand your first sentence, which is quite strange)

Where do I put the ExpiresDefault directive? I'm using CodeIgniter

I'm following the High Performance Websites book and the advice is to put this:
<FilesMatch "\.(gif|jpg|js|css)$">
ExpiresDefault "access plus 10 years"
</FilesMatch>
Where do I put it anyway? I'm using COdeIgniter. And I have access to my httpd.conf file.
You actually should put it in your .htaccess file in your site root.
Additionally, You can put it in the <Directory></Directory> block in your httpd.conf but it's probably better off in your .htaccess in my opinion.