Increase WordPress Speed Using .htaccess

Speed is a what makes your website more usable, optimized for conversions, more enjoyable to browse and loved by search engine algorithms.

.htaccess is a configuration file, usually found in the root of your web domain, that can increase your website security, but also significantly increase WordPress speed.

Gzip File Compression

Compressed files result in a reduced HTTP response times. Gzip compression zips your HTML documents, scripts and stylesheets. Note that images and PDF files are already optimized and crunched and should not be further compressed.

With file compression you save server bandwidth and decrease amount of time for browser to download the files.

Apache servers

mod_pagespeed is an Apache module developed by Google. It automatically applies web performance best practices and you should always check with your hosting company whether it supports it. While at it, ask them to assist you install and configure it. Once installed you can copy/paste the code below, to your .htaccess file.

<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters
extend_cache,combine_css,combine_javascript,collapse_whitespace,move_css_to_head
</IfModule>>

I know GoDaddy, DreamHost, EdgeCast, SiteGround and MediaTemple (not on their shared hosting plans) support the mod_pagespeed module, so if you’re with them, you can just insert the code to your .htaccess.

If your hosting company doesn’t support the mod_pagespeed module, then you can use the mod_deflate module. Just copy and paste the following piece of code to your .htaccess file.

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

Nginx servers

If your host uses Nginx servers, to enable gzip compression you need this code in your .htaceess

server {
gzip on;
gzip_types text/html text/css application/x-javascript text/plain text/xml image/x-icon;
}

Increase WordPress Speed using .haccess

Browser Caching

By setting browser caching you’re telling browsers to keep particular files for a specified period of time. When the file is needed again, the browser will pull from its local cache instead of requesting it from the server again.

Browser caching saves a lot of your bandwidth. A first-time visitor will make several HTTP requests to download all your website’s files, but by using the Expires and Cache-Control headers you can make those files cacheable. This avoids unnecessary HTTP requests on subsequent page views.

Apache Servers

It’s done through the mod_expires and mod_headers modules.

These rules go to .htaccess for setting Expires headers:

# BEGIN Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2500000 seconds"
ExpiresByType image/jpeg "access plus 2500000 seconds"
ExpiresByType image/png "access plus 2500000 seconds"
ExpiresByType image/gif "access plus 2500000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2500000 seconds"
ExpiresByType text/css "access plus 600000 seconds"
ExpiresByType text/javascript "access plus 200000 seconds"
ExpiresByType application/javascript "access plus 200000 seconds"
ExpiresByType application/x-javascript "access plus 200000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
# END Expire headers

and these are the .htaccess rules for setting up Cache-control header:

# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</IfModule>
# END Cache-Control Headers

Max-age directive is not necessary for Cache-Control header because it’s already set by the mod_expires module.

Nginx Servers

For Nginx this would be:

location ~* \.(jpg|png|gif|jpeg|css|js)$ {
expires 50h;
}

MySQL Database Size

WordPress 2.6 brought several improvements, autosave being one of them. Revisions of your posts and pages are being created while you type and this increases MySQL Database. The bigger the database, the higher the response time. You can either disable the revisions feature alltogether by inserting the following code snippet in your wp-config.php:

define('WP_POST_REVISIONS', false );

or by installing a plugin called Optimize Database after Deleting Revisions and setting a limit on allowed revisions per post.

Whichever you choose to do you’ll definitely feel the difference in WordPress speed.


There’s lots one can do to increase WordPress speed, using CDNs (content delivery networks) or applying performance best practices and using .htaccess is just one of many methods.

If you know and use other ways to speed up your WordPress website, use the comment area below and let us know.

Dragan Nikolic
I am the SEO & social media guy here at ThematoSoup, sharing tips on how to simplify your online business and make it more manageable.
Dragan Nikolic

@NikolicDragan

Co-founder & project manager at @ThematoSoup, interested in #minimalism and #WordPress. In love with nature.
RT @paulund: The Tuts+ Recommended Web Design Reading List - Tuts+ Web Design Article http://t.co/qO5YqS5mCU #WebDev - 5 hours ago
Dragan Nikolic
Dragan Nikolic
Dragan Nikolic

Latest posts by Dragan Nikolic (see all)

25 thoughts on “Increase WordPress Speed Using .htaccess

    1. John,

      With internet security, one can never be too paranoid. So yes, you can have both CloudFlare enabled and still work on increasing your WordPress security through .htaccess.

      1. Hi Dragan,

        Thanks for replying. I actually meant to ask if the speed tips for .htaccess are needed with Cloudflare.

        Thanks

        John

  1. Thanks for the tips but how much will this help with the page speed vs a cache plugins which i use w3t cache and Better WordPress Minify because i won’t mind removing them if the .htaccess method works just about the same way

  2. Dear Dragan, its a good article but I do not agree that “the bigger the database, the slower the response time”.

    If the amount of queries made when a user views a page is the same then the database size will not matter.

    1. Hey Tom, I believe you’re right, if a database is regularly defragmented and in a good shape.

      But still, every post revision requires a separate row in multiple wp_ tables. Removing older revisions frees up disk space and eases MySQL’s burden.

  3. Hi Dragan,

    I have started using Blogger along with WordPress, and i was wondering if the same can be used on this platform.

    Please shed some light on this issue!

    Piyush

  4. Hi, Not sure about Blogger, never used it. But, server side tweaks should work just fine and you need to be on a self hosted website in order to have access and make these changes.

  5. You can also increase website speed by lessening the homepage posts to 3. Do include the pagination numbers at frontpage to make navigation a breeze.

  6. Hi, Does it affect Google Adsense or Analytics codes in the website (because they have text/javascript codes) ? I really want to use it in my WordPress website. Please help. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>