There are many things you can and must do to work on your site’s SEO. Here I will share a quick “trick” to quickly boost your SEO with just a simple piece of code you need to add to your .htaccess file.
I will explain the different parts of this .htaccess snippet so you understand what is added. The main thing in these snippets is site speed’s improvement which is also a SEO booster. A quicker site is a happier visitor and a happier bot. By just using these .htaccess snippets you could boost your Google PageSpeed up to 20 points out of a 100!
Expire Headers
Expire Headers is another site speed and therefore SEO Booster. Expire Headers are added to tell the browser how often it has to fetch files afresh from the server based on a specific expiry data. And as a lot of static files do not change that often it is good to tell it when to do so. This is especially useful for static files like images.
# BEGIN Expire headers <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 7200 seconds" ExpiresByType image/x-icon "access plus 2592000 seconds" ExpiresByType image/jpeg "access plus 2592000 seconds" ExpiresByType image/png "access plus 2592000 seconds" ExpiresByType image/gif "access plus 2592000 seconds" ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds" # ExpiresByType text/css "access plus 2592000 seconds" ExpiresByType text/javascript "access plus 2592000 seconds" ExpiresByType application/x-javascript "access plus 2592000 seconds" ExpiresByType text/html "access plus 7200 seconds" ExpiresByType application/xhtml+xml "access plus 7200 seconds" </IfModule> # END Expire headers
Cache Control Headers
Cache Control Headers are added to tell the browser how long files should be cached before they should be loaded anew. This is important to make sure it does not reload files too often, but also to make sure it does fetch them anew after a certain amount of time. This is used by most modern browsers instead of Expire Headers and offers more options to control how long browser keep files and fetch anew
# BEGIN Cache-Control Headers <IfModule mod_headers.c> <FilesMatch "\\.(ico|jpe?g|png|gif|swf|gz)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> <FilesMatch "\\.(css)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> <FilesMatch "\\.(js)$"> Header set Cache-Control "max-age=2592000, private" </FilesMatch> <filesMatch "\\.(html|htm)$"> Header set Cache-Control "max-age=7200, public" </filesMatch> # Disable caching for scripts and other dynamic files <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$"> Header unset Cache-Control </FilesMatch> </IfModule> # END Cache-Control Headers
Gzip
Gzip is one technique Apache uses to compress files using one of its modules. By compressing the files they become smaller and are easier to download by the browser. Browsers deal with the decompressing on the fly.
# Gzip <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>
Deflate
Deflate is just another technique to compress files. Certain Apache servers only use deflate, others only Gzip. That is why we use the ifmodule statement to make sure it only works when the module is available.
#Deflate <ifmodule mod_deflate.c> AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript </ifmodule>
Featured Image: Paul Townsend https://flic.kr/p/oqznyf
Hi Jasper — and thanks for your post!
I have a follow-up question about compression. Let’s say that the Apache web server has both gzip- and deflate modules available.
You mention compression by gzip and deflate … which method is to prefer would you say (w.r.t performance)? Assuming for example gzip is to prefer, am I then right to say that the block of code for compression using gzip should appear earlier (above) than the block of code using deflate (in the htaccess file)?
Thanks!
If you are running Apache 1.3 you are using the old mod_gzip module. But most web servers these days work with Apache 2 and mod_deflate and gzip using the zlib library – see Siteground article here and Inmotion article here. Did a lot extra research on issues on deflate and gzip as a compression tool (Zoompf’s article especially very good) , but that would be a whole new article.
To answer your question, your server should be running either mod_deflate or mod_gzip. Using them together is not recommended and frankly I am not sure whether it would be possible as each Apache version seems to pick either mod_deflate or mod_gzip. Tip. So with the added code one of the modules will be used and compression will be executed.
To find out what compression module is running often command line access is needed as the info is often hidden for security reasons. Dreamhost on which most of my sites work, uses Apache 2.x and so mod_deflate. To test whether compression is working on your server and browser use http://www.whatsmyip.org/http-compression-test/