How to enable leverage browser caching?


I have tried to enable Leverage Browsing Cache and used this code in my .htacess file but even successful implementation of code available on the forums worldwide . i.e

i am still getting the message " Leverage browser caching for the following cacheable resources" in Google’s Page Speed Tools… How can i successfully implement leverage browsing cache in .htaccess file

The below link is the result of Page Speed Check of my Website

If would really appreciate if any tech wizard can provide me the working code that i can implement in the .htaccess file

Please check my website Reputation Solutions 4U and let me know what can be done to improve the loading speed. I have even minified JS and CSS

This is the result of Google Page Speed Check.

The code looks like it should work ok. Is the .htaccess file in the top level folder?

This is what I use for browser caching:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault                          "access plus 1 month"
  ExpiresByType image/jpg                 "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 text/html                 "access plus 0 seconds"
  ExpiresByType application/javascript    "access plus 1 month"
  ExpiresByType application/x-javascript  "access plus 1 month"
  ExpiresByType text/javascript           "access plus 1 month"
  ExpiresByType image/x-icon              "access plus 1 year"
1 Like

This is the .Htaccess script used on most of my sites that achieve very good optimisation results. Pity and infuriating that adding Google Analytics and/or Google Adsense reduces the overall score from 100% :frowning:

# ----------------------------------------------------------------------
# Environment Name
# ----------------------------------------------------------------------

# Sets the environment that CodeIgniter runs under.
# options: development, production, testing
  SetEnv CI_ENVIRONMENT production

# Compress all output labeled with one of the following MIME-types
  # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
  # and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines as
  # `AddOutputFilterByType` is still in the core directives)
  # OK <IfModule mod_filter.c>
    # REQUIRES mod_deflate.c
    # REQUIRES mod_zip.c
    AddOutputFilterByType DEFLATE application/atom+xml \
		                  application/javascript \
		                  application/json \
		                  application/rss+xml \
		                  application/ \
		                  application/x-font-ttf \
		                  application/xhtml+xml \
		                  application/xml \
		                  font/opentype \
		                  image/svg+xml \
		                  image/x-ico \
		                  image/x-icon \
		                  text/css \
		                  text/html \
		                  text/plain \
		                  text/x-component \
  # OK </IfModule>

# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------

# Use UTF-8 encoding for anything served text/plain or text/html
  AddDefaultCharset utf-8

# Force UTF-8 for a number of file formats
  # OK <IfModule mod_mime.c>
    AddCharset utf-8 .atom .css .js .json .rss .vtt .xml
  # OK </IfModule>

  # OK <IfModule mod_headers.c>
    <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
      Header set Cache-Control "max-age=12678400, public"
    <filesmatch "\.(html|htm)$">
      Header set Cache-Control "max-age=17200, private, must-revalidate"
    <filesmatch "\.(pdf)$">
      Header set Cache-Control "max-age=186400, public"
    <filesmatch "\.(js)$">
      Header set Cache-Control "max-age=12678400, private"
  # OK </IfModule>

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
# OK <IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On

  # If you installed CodeIgniter in a subfolder, you will need to
  # change the following line to match the subfolder you need.
  # MAYBE - RewriteBase /
    # RewriteBase /ci2/CodeIgniter4/public

  # MAYBE - Redirect Trailing Slashes...
    # RewriteRule ^(.*)/$ /$1 [L,R=301]

  # MAYBE - Rewrite " ->"
    # RewriteCond %{HTTPS} !=on
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    # RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
      # RewriteCond %{REQUEST_FILENAME} !-f
      # RewriteCond %{REQUEST_FILENAME} !-d
      # RewriteRule ^(.*)$ index.php/$1 [L]

  # MAYBE - Ensure Authorization header is passed along
    # RewriteCond %{HTTP:Authorization} .
    # RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# OK </IfModule>

# OK <IfModule !mod_rewrite.c>
  # If we don't have mod_rewrite installed, all 404's
  # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
# OK </IfModule>

# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
# OK <IfModule MAYBE_mod_deflate.c>
  # Force deflate for mangled headers 
    # OK <IfModule mod_setenvif.c>
      # OK <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
			RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    # OK </IfModule>
# OK </IfModule>

# OK <IfModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|ico|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
# OK </IfModule>

# OK <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/svg   "access 1 month"
  ExpiresByType image/x-icon      "access 1 year"
  ExpiresByType application/pdf   "access 1 month"
  ExpiresByType text/x-javascript "access 1 month"
  ExpiresByType application/x-shockwave-flash "access 1 month"
  ExpiresDefault "access 8 days"
# OK </IfModule>

# MOVED TO ENSURE mod-deflate Expires, Caching, etc works OK
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L]

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.