10

This question already has an answer here:

I have made some changes in .htaccess file present at root directory Default:

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

# Insert filter on all content
###SetOutputFilter DEFLATE
# Insert filter on selected content types only
#AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css 
text/javascript application/javascript application/x-javascript 
application/json image/svg+xml

# Netscape 4.x has some problems...
#BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
#BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
#BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Don't compress images
#SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary

</IfModule>

Updated:

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

# Insert filter on all content
SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css            
text/javascript   application/javascript application/x-javascript   
application/json image/svg+xml
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

</IfModule>

Also I have set minify js and css it is also not working.

enter image description here

But then also it showing as not complete in google page speed as well as gtmetrix

enter image description here

How should I resolve these issues? Thanks

marked as duplicate by Siarhey Uchukhlebau, liyakat, Prince Patel, Jai, Dhiren Vasoya Feb 17 '18 at 11:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • same issue on M2.2.1 – Goldy Nov 23 '17 at 5:16
  • if I am not correct, the first two commented lines are an 'either or' where you choose one or the other. Either to compress anything/everything or to only compress selected types. – Scott Jan 9 at 18:04
7
+50

I'm not an expert in this field, but I will share my experiences:

You need to have mod_deflate enabled in your Apache web server to utilize the compression declared in default Magento 2 .htaccess files.

This is indicated by <IfModule mod_deflate.c> tag in the .htaccess file in your Magento root folder - so it only compresses the files if mod_deflate is enabled.

If you're not the administrator of your hosting web server and mod_defalte isn't turned on, you should ask your hosting provider to enable this for you.

If they can't/won't, then an alternative is to enable zlib.output compression in your php.ini - which isn't as effective as mode_deflate or mod_gzip.

This can be done if you have access to php.ini --> find zlib.output compression in it and turn it on (zlib.output compression = On).

But this does not affect minification or merging of css and js files which can be set through Magento 2 admin.

Also keep in mind that minification only works in production mode, so you can't really test it in developer mode (not sure about default mode).

To turn on gzip compression you can add this to your .htaccess

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|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.*
</ifModule>

And this also depends on the Apache mod_gzip.

Also I suggest not to use both gzip and deflate because it will most probably slow down your site if used together. So you should only choose 1 of them.

5

Compare your htaccess code it must contains.

<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"

   #ExpiresDefault "access plus 1 year"
   ExpiresByType text/html A0
   ExpiresByType text/plain A0
</IfModule>

<IfModule mod_deflate.c>
 # Compress HTML, CSS, JavaScript, Text, XML and fonts
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
 AddOutputFilterByType DEFLATE application/x-font
 AddOutputFilterByType DEFLATE application/x-font-opentype
 AddOutputFilterByType DEFLATE application/x-font-otf
 AddOutputFilterByType DEFLATE application/x-font-truetype
 AddOutputFilterByType DEFLATE application/x-font-ttf
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE font/opentype
 AddOutputFilterByType DEFLATE font/otf
 AddOutputFilterByType DEFLATE font/ttf
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/xml

 # Remove browser bugs (only needed for really old browsers)
 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4\.0[678] no-gzip
 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 Header append Vary User-Agent
</IfModule>

FileETag none
  • This issue was solved, I will check your answert next time. Thanks for reply. Can you just explain what was issue? – Nitesh Dec 29 '17 at 8:48
  • 1
    @Magento2Learner That issue is some what resolved by enabling deflate mode by server support and also check site in production mode. – Nitesh Feb 1 '18 at 18:47
1

For better optimization as per google page insight please follow below points

  1. Do not merge JS files only minify JS file

  2. Minify CSS files

  3. Optimize image files as possible level

  4. Defer JS (Best plugin for defer JS)

  5. Defer CSS as many as you can

0
  1. Go to Magento_Folder/var/log/ and delete all log files
  2. Compress all images

I hope its work for you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.