How To Enable GZIP Compression for Magento
GZIP compression is a standard method to compress files for faster webpage loading.
When a visitor visits the site, the server is called to deliver the requested files. If these files are larger in the size, it causes a higher load time.
Bigger the file, longer it takes to display the result to the visitor.
With GZIP compression, the files are compressed before sending them to the browser, resulting in reducing the load time.
According to GTmetrix, GZIP compression reduces the size of pages and style sheets by up to 70%! And thus, optimizing your webpages by enabling GZIP compression should be your priority if you want to outrank the competitors and offer a smooth user experience. And for Magento 2 and Magento stores, it is not an option to ignore performance optimization.
Ecommerce platform must be optimized for quick shopping owing to the cutthroat competition. And as a store owner, you can just start with the below method to enable GZIP compression for Magento stores!
Method To Enable GZIP Compression for Magento:
Make sure mod_deflate
is on in Apache. You can check by creating an info.php
file and calling phpinfo();
. It will output server PHP / Apache specs in the browser. Don’t forget to remove it when you’re done!
Then add the following to your htaccess
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
<IfModule mod_php5.c> ## enable resulting html compression php_flag zlib.output_compression on </IfModule> <IfModule mod_php7.c> ## enable resulting html compression php_flag zlib.output_compression on </IfModule> <IfModule mod_deflate.c> ## Force compression for mangled `Accept-Encoding` request headers <IfModule mod_setenvif.c> <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 </IfModule> </IfModule> ## Compress all output labeled with one of the following media types. <IfModule mod_filter.c> AddOutputFilterByType DEFLATE "application/atom+xml" \ "application/javascript" \ "application/json" \ "application/ld+json" \ "application/manifest+json" \ "application/rdf+xml" \ "application/rss+xml" \ "application/schema+json" \ "application/vnd.geo+json" \ "application/vnd.ms-fontobject" \ "application/x-font-ttf" \ "application/x-javascript" \ "application/x-web-app-manifest+json" \ "application/xhtml+xml" \ "application/xml" \ "font/eot" \ "font/opentype" \ "image/bmp" \ "image/svg+xml" \ "image/vnd.microsoft.icon" \ "image/x-icon" \ "text/cache-manifest" \ "text/css" \ "text/html" \ "text/javascript" \ "text/plain" \ "text/vcard" \ "text/vnd.rim.location.xloc" \ "text/vtt" \ "text/x-component" \ "text/x-cross-domain-policy" \ "text/xml" </IfModule> ## Map the following filename extensions to the specified ## encoding type in order to make Apache serve the file types ## with the appropriate `Content-Encoding` response header ## (do note that this will NOT make Apache compress them!). <IfModule mod_mime.c> AddEncoding gzip svgz </IfModule> </IfModule> |
That’s it.
If you are looking for other ways to optimize the Magento 2 store performance, follow the checklist at How do I Speed Up Magento 2
Please mention any doubts on the topic in the Comments section below. I’d be happy to help.
Boost the Magento 2 store speed and enjoy the top rank because site speed is surely one of the important factors to decide the same for the search engines!
You may share this post on social media to help the community optimize their Magento 2 stores!
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
Hello
When I try to implement these changes on my Magento 2 website I got the following error.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Hello Vivek,
Make sure mod_deflate is on in Apache. You can check it by creating an info.php file and calling phpinfo().
It will output server PHP / Apache specifications in the browser.
Thank you.
thanks for the information
Happy to help 🙂
The speed of online store always play a vital role in sales ratio. There are many factors to speed up Magento store and yes, enabling GZIP Compression is one of them.
Thanks for sharing this useful guide!
Thanks for the appreciation!