Jay
Jay

Reputation: 841

leverage browser caching .htaccess not working

I am using laravel 5, for speed I test in google speed insight, its showing leverage browser caching

to solve that I have used following .htaccess code but non of them are working

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
</IfModule>

<IfModule mod_headers.c>
# Serve gzip compressed CSS files if they exist 
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.css" "$1\.css\.gz" [QSA]

# Serve gzip compressed JS files if they exist 
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.js" "$1\.js\.gz" [QSA]


# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
RewriteRule "\.js\.gz$" "-" [T=text/javascript,E=no-gzip:1]


<FilesMatch "(\.js\.gz|\.css\.gz)$">
  # Serve correct encoding type.
  Header append Content-Encoding gzip

  # Force proxies to cache gzipped & 
  # non-gzipped css/js files separately.
  Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>

<IfModule mod_expires.c>
ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"

# Media: images, video, audio
ExpiresByType assets/images "access plus 1 month" 

# CSS and JavaScript
ExpiresByType assets/css "access plus 1 month"
ExpiresByType assets/js "access plus 1 month"

<IfModule mod_headers.c>
 Header append Cache-Control "public"
</IfModule>

</IfModule>

Second code that I used in .htaccess is this one

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
ExpiresByType text/html "access plus 1 second"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 604800 seconds"
ExpiresByType application/x-javascript "access plus 604800 seconds"
</IfModule>

I also tried this one code in .htaccess to make it work, but this one is also not working

<IfModule mod_expires.c>
# Set Cache-Control and Expires headers
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "max-age=2592000, public"
Header set Expires "1 day"
</filesMatch>
<filesMatch "\\.(css|css.gz)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js|js.gz)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm)$">
Header set Cache-Control "max-age=7200, public, must-revalidate"
</filesMatch>
</IfModule>

I have used all above code but I got same result every time should fix: Leverage browser caching

on most of the anaylser.

is anything I am missing in this

Upvotes: 1

Views: 1503

Answers (2)

DragonFire
DragonFire

Reputation: 4082

If You are in ubuntu try this

sudo a2enmod expires
sudo systemctl restart apache2

Upvotes: 2

Jay
Jay

Reputation: 841

Check that modules are enable in server.

If modules are not enabled than code will not work.

Upvotes: 1

Related Questions