Adding Expires Headers and HTML Compression | htaccess Example

If you have ran an SEO report of your site and got some results about how you need image expire headers and html compression, or if you would like to increase the speed of your website then the following tutorial should help you drastically.

It applies to apache servers and requires a couple Apache modules for it to work. BlueHost shared servers are already configured so once again, we have to give them our recommendation!

Apache Module mod_expires

Adding the expires headers to your site’s htaccess file allows a browser to know that files such as images are static and do not need to be refreshed on every page load. It is easy to implement just by adding the following to your htaccess file (A complete .htaccess file can be found at the bottom of the page). You can modify the times as needed and also the files. For more information on Expires Headers and the Apache Module mod_expires visit: http://httpd.apache.org/docs/current/mod/mod_expires.html

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
</IfModule>

Apache Module mod_deflate.c

The mod_deflate Apache module compresses files for browsers. Using Apache mod_deflate to compress HTML, text or XML files can decrease the delivered file size by up to 70%. The compression of files will increase server load, but it is tiny compared to decrease in loads when responding to browser requests. For more information you can visit, http://httpd.apache.org/docs/current/mod/mod_deflate.html. Below you can see an example of how to use the mod_deflate module in Apache. Just add this to your .htaccess file.

<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Optimized .htaccess file example for WordPress.

# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000<
ExpiresByType image/ico A2592000
</IfModule>

<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Comments are closed.