I am working on SEO and need to verify that Image Headers Expires is working. Both modules for expires and headers are installed and enabled.
Any ideas?
The .htaccess file has this in it
<IfModule mod_expires.c>
ExpiresByType application/javascript "access plus 2 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 2 days"
</IfModule>
I have added this to both the wordpress/ directory and the wordpress/wp-content/ directory.
And yet the SEO tool is saying "Your server is not using expires header for your images."
to test using curl I have done
Date: Sun, 15 Jan 2023 04:31:01 GMT
Server: Apache
Location: https://www.jaycocioservices.com/about
Content-Type: text/html; charset=iso-8859-1
You are missing the ExpiresActive directive to enable the generation of the relevant Expires and Cache-Control headers.
You don't need the <IfModule> wrapper - this is only going to mask the error if mod_expires is not installed.
You don't need the ExpiresByType image/jpg ... directive since the correct mime-type is image/jpeg, which you already have in the directive that follows.
You don't need to repeat these directives in both the /wordpress and /wordpress/wp-content directories. Placing these directives in the root .htaccess file should be sufficient and naturally applies to all subdirectories.
Also consider setting an ExpiresDefault directive for all other mime-types.
For example:
ExpiresActive On
ExpiresByType application/javascript "access plus 2 days"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 2 days"
ExpiresDefault "access plus 1 day"
to test using curl I have done
Date: Sun, 15 Jan 2023 04:31:01 GMT
Server: Apache
Location: https://www.jaycocioservices.com/about
Content-Type: text/html; charset=iso-8859-1
That looks like a redirect response (due to the presence of the Location header), which perhaps should not be cached anyway (or left to the browser default in the case of a 301). Make sure you are requesting the canonical URL to test.
This (/about text/html) is also for a request not covered by your existing mod_expires directives. You need to be requesting an image, CSS or JS file.
Related
Probably quite a basic question but I want to be 100% sure...
It's recommended to place "Add Expire Header" rules in your htaccess file, but my question is that, the HTML text within my site changes very often, i.e. written content - so, just to be clear, adding the below is completely fine given that my content (text) changes regularly?
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
I want repeat visitors to see my fresh content of course, hence why I'm asking
Thanks
No, that's not completely fine given your requirement:
I want repeat visitors to see my fresh content
If you serve an HTML page with headers that indicate that the client can cache that response until (now + two days), then if the user refreshes that page, for the next two days the browser will return the cached HTML.
I run a server with several websites on it. I would like to implement default cache control behavior for all of these websites. Can I just do this by adding the following code to httpd.conf or must I make the changes in the configuration of each virtual host separately?
<IfModule mod_expires.c>
ExpiresActive on
# Your document html
ExpiresByType text/html "access plus 12 hours"
# Media: images, video, audio
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# CSS and JavaScript
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
# Default
ExpiresDefault "access plus 1 month"
</IfModule>
Documentation can be found in the Apache HTTP Server web site. In the "Documentation" section on the left pane, click on your server version. I'll assume 2.4. Since you are looking for reference on specific directives, you can click on the Directives link. You'll get an alphabetical index.
I won't copy the complete information, just a little sample for ExpiresActive. The important bit is the top table:
Description: Enables generation of Expires headers
Syntax: ExpiresActive On|Off
Default: ExpiresActive Off
Context: server config, virtual host, directory, .htaccess
Override: Indexes
Status: Extension
Module: mod_expires
At "Context" we can read that the directive can be set at several places, including server config. So this answers your question: in theory, it should work. You should check the rest of the directives in order to make sure (or just test it).
I can't get the expire header on JS files to work properly.. It works fine on eg CSS files!?
Could anybody tell me what Im doing wrong? :(
htaccess
ExpiresActive on
ExpiresDefault A0
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType text/javascript A29030400
ExpiresByType application/x-javascript A29030400
header on CSS file
Cache-Control max-age=29030400
Connection Keep-Alive
Date Sun, 14 Oct 2012 12:06:45 GMT
Etag "be-4cbcf2ad48b40"
Expires Sun, 15 Sep 2013 12:06:45 GMT
Keep-Alive timeout=5, max=97
Server Apache/2.4.2 (Win64) PHP/5.4.3
header on JS file
Cache-Control max-age=0
Connection Keep-Alive
Date Sun, 14 Oct 2012 12:06:45 GMT
Etag "1ad-4cc00d069f493"
Expires Sun, 14 Oct 2012 12:06:45 GMT
Keep-Alive timeout=5, max=99
Server Apache/2.4.2 (Win64) PHP/5.4.3
(can't comment yet...)
I run the following settings and it works.
ExpiresByType text/js "access plus 1 years"
ExpiresByType text/javascript "access plus 1 years"
ExpiresByType application/javascript "access plus 1 years"
ExpiresByType application/x-javascript "access plus 1 years"
Should work
Cheers
Robin
Try wrapping an ExpiresDefault in a <filesMatch tag instead of using ExpiresByType :
<filesMatch "\.(js)$">
ExpiresDefault A29030400
</filesMatch>
You might need to activate the Expires module from inside your Apache host configuration.
In my case, I added
ExpiresActive On
right before the first ExpiresByType directive and it started working instantly (after an Apache reload).
I'm not sure why this needs to be explicitly enabled. On other servers it used to work as soon as I enabled the mod with a2enmod (maybe with Apache 2.2, but I can't find a clear pattern)
Old question but here's what I learned:
Press F12 in your browser (I'm using Chrome) and go to the "Network" tab and look for the javascript that's not getting cached. Look for the header information there (via a "Headers" tab, for example) and look at the "content-type" in the response header information.
In my case it was "application/javascript", which I hadn't defined specifically in my .htaccess. Once I added that line:
ExpiresByType application/javascript "access plus 2419200 seconds"
Then javascript was getting cached as expected.
1- Is this a valid cache control header:
Cache-Control: max-age=86400, s-maxage=3600
2- I am trying to set cache control headers for objects served from S3 through CloudFront. The images don't often change, but sometimes can. Does this seem like a good cache control for this? CloudFront should re-validate every 60 minutes, but the user will cache for a full day. (I'm less worried about an individual user and more worried about continuing to serve the stale image to new users).
3- Are there other headers I should use? (ie. proxy-revalidate)
Its not horrendously easy to change these settings on S3 objects so I'm trying to get it right the first time. Thanks.
I just made it working with both
Access-control-Max-Age (Set in S3 CORs setting)
Cache-control
expires
In theory and from AWS S3 / Cloudfront doc, only 1. will works. In practice, it takes both.
Keith
Here's what I got to work:
Cache-Control: max-age=29030400, public
Expires: Wed, 30 Dec 2015 18:00:00 GMT
In theory, you only need one or the other. But this whole browser caching thing with S3 and CloudFront has been a big pain in my backside, so I like to keep them both in there. And I also put the following in my .htaccess file:
# BEGIN Caching
Header set Cache-Control "max-age=29030400, public"
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/plain "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END Caching
okay, I'm still trying to get my head around some of the caching stuff and I have gone through a couple of examples I could find on Google. I have added the following code to my .htaccess file:
### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif "access plus 3 months"
ExpiresByType image/png "access plus 3 months"
ExpiresByType image/jpg "access plus 3 months"
ExpiresByType text/javascript "access plus 3 months"
Using the Chrome audit tools and the YSlow Firebug tool, it looks like this is caching some of my images/files, but not by far all of them. I still have a list of files (.jpg, .js and .css - I know I've not set the css files to cache here) that aren't caching. The message in the Chrome audit simply states The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:
some of the images that aren't caching are background images, others are part of a js gallery and they're being called via the JS - could that be affecting why they aren't caching?
Sorry I can't give a link to the code - the sites still under wraps and limited to client view only.
Thanks in advance!
It looks like you've written the MIME-types wrong:
# enable expirations
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/pjpeg "access plus 1 week"
ExpiresByType text/javascript "modification plus 1 week"
ExpiresByType application/javascript "modification plus 1 week"
ExpiresByType text/css "modification plus 1 week"