Posted on

Increase WordPress Speed Using .htaccess

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 need help speeding up your WordPress website, you can get in touch using the contact form and I’ll try to help you.

Free Marketing Updates
Get the best marketing practices in your inbox.
We always respect your privacy.

Share
Dragan Nikolic
I am a co-founder and editor at ThematoSoup, sharing marketing best practices, tips on how to simplify your online business and make it more manageable.
Dragan Nikolic
Hey @tj_reinhart do you accept new affiliate partners on Sumo? - 5 years ago
Dragan Nikolic

57 comments 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

  7. It works!

    My sites’s grade via YSlow jumped from C to B upon implementing the above.

    Thank you so much, Dragan Nikolic!

  8. Pingback: WordPress SEO Optimization In Website – WordPress Team Support
    1. You need to do many things, some of which are:
      Eliminate render-blocking JavaScript and CSS in above-the-fold content
      enable compression, reduce server response time, enable browser caching, optimize images, minify JS, minify CSS and HTML, get rid of render-blocking JavaScript and CSS above-the-fold content…

  9. Pingback: WordPress-SEO Magic: Tapping the Unlimited Potential | ThematoSoup
  10. By disabling hotlinking and compressing images increases site speed considerably. Google page speed cdn can be counter productive as it sometimes does not load the advertisements and it has huge impact on shared hosting.

  11. I got an increase from the Google Page Speed test from 40/100 to 86/100.

    I also used website auditor from Link-assistant.co.uk/wsa (dont know if links are allowed) but you can always copy/paste – it tells me exactly where my issues are – not only with page speed, but also with HTML errors, 404s as well as all the SEO type info re h1 headers, page titles etc.

    I will have to check to see if analyses the .htaccess file as well.

  12. In which format do you prefer the images on your website? Most of the times I use images in png format. Does this have some kind of an impact on the speed of the website?

    1. It depends on the type of the image. If an image is heavy with graphics and colors, you can use JPEGs, for every other instance you can use PNG.

      Thanks for stopping by.

  13. Pingback: SEO friendly website design structure for WordPress Site - BizWp
  14. Very useful data, I was also looking for something that would speed up the website for me a bit. So how do we use it will also reduce the consumption of our pages because this part will already be in the browser’s memory?

  15. Pingback: gazette of india

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.