Jump to content

[Solved] mod_gzip and mod_deflate


thorast

Recommended Posts

Hello, i am trying to improve the speed of my web, so i am testing it in ttps://developers.google.com/speed/pagespeed/insights, then i see that there are two modules mod_gzip and mod_deflate that can be installed in Apache and other servers to compress the contents that send to the client so the files are smaller and this is very good for the server too.

I am seeing that other servers have this enabled by default.

My question it is, tommy has this enabled or installed?, if yes how can i do for use it?.

Thanks in advance, 

Edited by thorast
Link to comment
Share on other sites

you can enable it from cPanel > Optimze Website. If you want to do it manually you have to include following code to your .htaccess file:

<IfModule mod_deflate.c>
    AddOutPutFilterByType DEFLATE text/xml text/css font/opentype font/otf text/javascript font/ttf application/vnd.ms-fontobject application/x-font-opentype application/x-font-otf application/x-font-ttf application/x-font-truetype application/x-font application/javascript text/plain image/x-icon image/svg+xml application/x-javascript application/xhtml+xml application/rss+xml application/xml
    <IfModule mod_setenvif.c>
        # 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

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

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

    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>
# Set server time zone as Dhaka, Bangladesh
SetEnv TZ Asia/Dhaka
# Cache for speeding up
<FilesMatch "\.(?i:gif|jpe?g|png|ico|css|js|swf|xml)$">
  <IfModule mod_headers.c>
    Header set Cache-Control "max-age=864000, public, must-revalidate"
  </IfModule>
</FilesMatch>
<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=63072000; includeSubDomains"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-Xss-Protection "1; mode=block"
Header set X-Content-Type-Options "nosniff"
</IfModule>
<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>
Link to comment
Share on other sites

Woooow! i am impressed! now my webpage it is giving an score of 92/100 in phones and 95/100 in pc!!! 

 

My .htaccess it is now like this, noticed that first part is for forced https if people writte only the domain

# Set server time zone as Europe, Madrid
SetEnv TZ Europe/Madrid

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_deflate.c>
    AddOutPutFilterByType DEFLATE text/xml text/css font/opentype font/otf text/javascript font/ttf application/vnd.ms-fontobject application/x-font-opentype application/x-font-otf application/x-font-ttf application/x-font-truetype application/x-font application/javascript text/plain image/x-icon image/svg+xml application/x-javascript application/xhtml+xml application/rss+xml application/xml
    <IfModule mod_setenvif.c>
        # 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

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

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

    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

# Cache for speeding up
<FilesMatch "\.(?i:gif|jpe?g|png|ico|css|js|swf|xml)$">
  <IfModule mod_headers.c>
    Header set Cache-Control "max-age=864000, public, must-revalidate"
  </IfModule>
</FilesMatch>
<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=63072000; includeSubDomains"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-Xss-Protection "1; mode=block"
Header set X-Content-Type-Options "nosniff"
</IfModule>
<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

Thank you MRJ, have a nice day!!!

Edited by thorast
Link to comment
Share on other sites

I am glad to help you. I did some hard calculations and googling for getting a faster, more optimized, and little bit secured site with just a single htaccess file. I had to browse some speed testing site and some tutorials. Anyway, Good luck with your site.

Link to comment
Share on other sites

Just a heads up on that htaccess...forcing the www is a bad idea. Not only is it a needless redirect, forcing it can lower your ranking on search engine and will break subdomains if their root folder is inside the folder containing this htaccess. In addition, there are (typically older) scripts that rely on not having a www in front (no idea why, but I used to use one).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...