Apache Fix Leverage Browser Cache - apache

I have this code to fix leverage brower cache in my websites for apache, but I think mod_expires is not enabled
# Leverage Browser 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"
ExpiresDefault "access 1 month"
</IfModule>

Related

Apache Not Cacheing Images

# 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 application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
I am using the above code in my .htaccess, but Page Speed Insights, GtMetrix etc are still saying my images not being cached. I tried various different examples posted and nothing is working. My host saids the issue is outside their scope... Any tips or direction to go in to try and diagnose the issue?

Leverage Browser Caching js?ver=

I'm facing an odd problem with my Leverage Browser Caching. In my .htaccess I use the following:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
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 ##
This successfully caches my website. But the issue I'm facing is it is missing out some non-external JS files:
http://example.com/wp-content/themes/tracks/js/build/production.min.js?ver=4.7.2 (2 days)
http://example.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1 (2 days)
http://example.com/wp-includes/js/jquery/jquery.js?ver=1.12.4 (2 days)
http://exmaple.com/wp-includes/js/wp-embed.min.js?ver=4.7.2 (2 days)
http://example.com/wp-includes/js/wp-emoji-release.min.js?ver=4.7.2 (2 days)
I tried to fix it by adding the following lines, but the result was still the same:
ExpiresByType text/javascript “access plus 1 month”
ExpiresByType application/javascript “access plus 1 month”
ExpiresByType application/json “access plus 1 month”
ExpiresByType application/x-javascript “access plus 1 month”
Any idea why it is not picking up the js?ver= and how to fix this problem? In case it helps:
Hosting: Hostinger
Server: Apache 2.4
OK, I managed to fix the issue. I was correct in using the following four lines:
ExpiresByType text/javascript “access plus 1 month”
ExpiresByType application/javascript “access plus 1 month”
ExpiresByType application/json “access plus 1 month”
ExpiresByType application/x-javascript “access plus 1 month”
However, the quotation marks around the access plus section are for some reason curly quotations. This was therefore returning 500 and 503 server errors. After replacing the quotation marks it has since fixed my issue and now correctly caches js?ver= files too.
My final Leverage Browser Caching code looks like this:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
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 application/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/json "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

Leverage Browser caching working For css and javascript but not for images

In my website in htaccess I added
## 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/javascript "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
After testing my site with
https://developers.google.com/speed/pagespeed/insights
It is showing Leverage browser caching is working for css and javascript but not for images (both jpeg and png)
Please tell me if there is any error in above htaccess

Leverage Browser Caching in .htaccess not working

I have this in my .htaccess file.
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month”
# Data interchange
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rdf+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/ld+json "access plus 0 seconds"
ExpiresByType application/schema+json "access plus 0 seconds"
ExpiresByType application/vnd.geo+json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
# Favicon (cannot be renamed!) and cursor images
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 week"
# HTML
ExpiresByType text/html "access plus 2 day”
# JavaScript
ExpiresByType application/javascript "access plus 1 month”
ExpiresByType application/x-javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
# Manifest files
ExpiresByType application/manifest+json "access plus 1 year"
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Media files
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/bmp "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 image/svg+xml "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"
# Web fonts
# Embedded OpenType (EOT)
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType font/eot "access plus 1 month"
# OpenType
ExpiresByType font/opentype "access plus 1 month"
# TrueType
ExpiresByType application/x-font-ttf "access plus 1 month"
# Web Open Font Format (WOFF) 1.0
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
# Web Open Font Format (WOFF) 2.0
ExpiresByType application/font-woff2 "access plus 1 month"
# Other
ExpiresByType text/x-cross-domain-policy "access plus 1 week"
</IfModule>
But on gtmetrix it still says:
The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources
The syntax of your htaccess file is correct. Please make sure you
place the htaccess file in the right folder (i.e. the root folder of your project)
have the module mod_expires installed and enabled (see how to check if mod_expires is enabled
have set the desired expire time for all of your resources (which resources are ment by "the following resources"? You've set the expire header up to a year for some resources)

Add Expires headers for files with query string

I have added Expire Headers to my .htaccess:
## EXPIRES CACHING ##
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 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"
ExpiresDefault "access 2 days"
## EXPIRES CACHING ##
But gtmetrix says "There are 30 static components without a far-future expiration date."
I realized that all the files with query string are not included to be cached. For example:
history.js?ver=1.7.1
history.adapter.jquery.js?ver=1.7.1
jquery.form.min.js?ver=3.48.0-2013.12.28
respond.min.js?ver=3.8.1
How can I add caching for those files?
Thanks.
Afaik the query string is ignored by mod_expire
However you may have to include more types, e.g.
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
Also wildcards can help you
ExpiresByType image/* "access plus 1 year"