.htaccess code for faster page loading

I’ve been experimenting with changing various Apache server settings via the .htaccess file in my quest to get my WordPress sites to load more quickly. Obvously, if you’re made of money you simply host your site(s) at WP Engine, but for economic pygmies such as myself, a hosting multiple sites under one inexpensive Dreamhost, Hostgator, or Bluehost account is the way to go.

However, you often can fine-tune Apache to speed up the way your host serves your site, and this is how I did it.

If you have a normal WordPress install, WordPress adds the following code to your .htaccess file (which is in the root directory of your WordPress install):

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
 
# END WordPress

Right below this, add this additional code:

## BEGIN DEFAULT CHARACTER SET ##
AddDefaultCharset UTF-8
## END DEFAULT CHARACTER SET ##

## BEGIN EXPIRES CACHING ##
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"
## END EXPIRES CACHING ##

## BEGIN GZIP COMPRESSION ##
# compress text, HTML, JavaScript, CSS, and XML
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
# remove browser bugs
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
## END GZIP COMPRESSION ##

## BEGIN CONTROL E TAGS ##
FileETag none
## END CONTROL E TAGS ##

According to GTMetrix, Pingdom, WebPageTest, and YSlow, there are only modest improvements. When you visit your site, however, you’ll notice that it is much more responsive. By the way, each “speed test” tool comes up with different numbers, as well as different recommendations about what to do to improve your speed. Therefore, you have to take each with a grain of salt. By comparing results, though, you can see if you are making progress — especially if you pay attention to what each tool says it sees in the HTML headers.

While you’re doing all of this, don’t forget to set up your site(s) with Incapsula.com, both as a safeguard against various types of attacks, and as a CDN.

Advertisements