How to: Enable HTTP Keep-Alive in WordPress?

We usually enable HTTP keep-alive because it responses to header by keeping the client/server connection open across multiple server requests. The open connection improves the web performance when a client makes multiple server requests for webpage content, because the server can return the content for each request more quickly rather than processing all requests at once. Or if we don’t enable HTTP Keep-Alive, the server got to open a new connection for every single request.

HTTP Keep-alive

Locate the Code in .htaccess

To enable HTTP keep-alive response header in your WordPress blog all you have to do is to locate the following line codes in your blog’s .htaccess file. Or if you don’t know how to edit .htaccess file in WordPress, click here.

<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>

Make sure you have place the above codes after line #END WordPress. Yep, you did it.

Related  Top 50 Best Social Networking WordPress Plugins

63 Comments

  1. Nice article. Its rare to find a post that explains the use of KeepAlive from a practical yet conversational perspective. Most sites just regurgitate whats already said on the apache site. Thanks.

      1. Thanks buddy for this useful post, actually my website was suffering with slow speed issue and here you made it easy to fix. Thanks once more.

  2. Thank you for this useful tutorial. It work on my blog
    Previously my blog score on GTMETRIX is 97. Now the score is 99 on every single page 😀

  3. Hi, this is my first comment on a blog ever!
    I just felt like I had to give you a huge THANK YOU for this! My GTmetrix score shot through the roof….so happy!

  4. Brother haroon, can you provide me your email address? i have added this code to the .htaccess file but still facing the issue. GTmetrix shows that keep alive is not enabled. If you could help me out i would be indeed very much happy.

  5. In you Mohammad, I have discovered the tech person to help me in technical issues related to WordPress. All along I was looking for a way to enable keep alive in WordPress. I read lengthy articles which confused me even more. But when I landed on this article, I was glad to see a simple and well explained process of how to enable keep alive even with 3W Total Cache installed. Thank you for being a nice teacher.

    This has been a great achievement for me today. My GTmetrix score has increased from 81% to 92% (from B to A?). The Pingdom score has also increased to 92%.

    I still have other related issues to work on to improve further my website speed performance. I believe, I will get the answers right here at this site. I have sincerely appreciated. You have made my day. Thanks for this tutorial. Keep it up Mohammad! Bravo!

    1. Well, you are right and you can get many articles related to other WordPress issues on my site that will definitely help you @Charles. Keep visiting us for more informative articles. Thanks

  6. Dear Mohammed Bhai,

    Currently my code in .htaccess looks like this…

    # BEGIN W3TC CDN

    Header set Access-Control-Allow-Origin “*”

    # END W3TC CDN
    # BEGIN W3TC Browser Cache

    Can you please help.. What should I put in .htaccess file and exactly where… I am a bit confused..

      1. Muhammad Bhai … Thanks A Lot.. This worked like a Gem 🙂 .. And Wish you a Very Happy Ramzan Bhai 🙂

  7. I saw this on the GTmetrix website so thought I’d check it out… It definitely worked. It seemed to improve my score by about 15%, thanks Muhammad 🙂

  8. Hello I am a real ht access newbie! This page is really helping though. .Can you just confirm do I place the line of code before or after # END WordPress?
    Thank you so much!

  9. If I have RewriteCond after my # END WordPress, do I put the above code before or after the multiple RewriteCond I have?

  10. Hi!

    I just tried this but it doesn’t seem to work. Can’t really figure out the problem though. I put it at the end of my htaccess, using the Yoast SEO plugin’s file editor. Does this take a while to get noticed by gtmetrix or do you have a clue what else could be the problem?

    thanks a lot!

  11. Hi,

    I added into .htaccess file . but it’s not working. can you please provide another way or something

    thanks

  12. I’ve translated my website with WPML, now GTmetrix is telling me to add the keep-alive code into the htaccess. But it’s already in there and it does work for my base language, could you tell me why it’s not working on the secondary language?

    Thanks!

  13. Hi there Muhammad. I made the changes that you laid out to help the pagespeed by using the keep alive code but i still have a very bad pagespeed, specifically for the keep alive section as indicated by gtmetrix.

    Does it take a while for the changes to actually be applied? or is there something else that i need to do? Thanks for your help

    1. GTmetrix advise not to add Keep-Alive to the .htaccess file as it gives out false information to browsers. If you down have access to http.conf there is nothing you can do.

  14. Hello. I used this code a few days ago, but gtmetrix is telling me the same (F score for “Keep alive”). Can you help me, please? You can see the website by clicking on my name. Thank you. 🙂

  15. Hey Muhammad, thanks for this post. This error was coming up during speed test. I have put that entry in .htaccess and now the error has gone.

  16. Enabling Keep-Alive connections in Apache

    Apache enables Keep-Alive connections by default, however you can explicitly turn them on by adding the following line to your httpd.conf file.

    KeepAlive On

    Having said that, if your site is running on a shared host, you probably won’t have access to httpd.conf and will therefore have to live with whichever setting your hosting provider has chosen. Contrary to what some websites suggest, adding a Header set Connection keep-alive directive to your .htaccess file will not actually enable Keep-Alive connections, but it will send browsers misleading information about the server’s capabilities.

    Do not turn on Keep-Alive in the .htaccess file.

Leave a Reply

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