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.
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.
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.
Well, thanks for stopping by @Taicmc. Keep visiting us for more helpful stuff…
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.
Hi Muhammad, thanks so much for this, just what i needed! My GTmetrix score went from 79 to 92 page load speed.
Cheers.
Your welcome @Julian brother. It’s my pleasure.
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 😀
Thanks mate. My score now in google is 95 🙂
That’s my pleasure @Sandeep brother.
Thanks for the code.
Hey @Plum, hope the code helped. By the way I thank you for stopping by and leaving positive comment.
Thank you, I managed to satisfy
It’s fine @ kursus mobil… Your welcome and keep reading my blog.
Thanks…..
This working..
my http://www.4um.in site up to 94 % ….
thanks Muhammad Haroon..
That’s my pleasure @Mohamed Rafi.
Hi,
what if I have W3 Total Cache installed, would it still help to add these lines at the end? please let me know.
Hi @Anupam,
Yeah, these line of codes will do there own work after all W3 Total Cache caches your blog in it’s own directions.
Regards,
Admin
Thanks. I needed this
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!
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.
This is all I can help with this article or if you need premium service you will need to contact me. Or try this.
How to Make expiry for header?
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!
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
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..
Hi @Aashish bro,
You only need to place the block of code from this article to the end of your .htaccess file, don’t be confused. And if you don’t know how to edit .htaccess file, click here.
That’s all.
Muhammad Bhai … Thanks A Lot.. This worked like a Gem 🙂 .. And Wish you a Very Happy Ramzan Bhai 🙂
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 🙂
Thanks for the tutorial, I used it and my speed score went up from 78% to 84%!
You are welcome @William. Keep visiting us for more interesting and useful stuff.
Thanks for this helpful article! Your advice worked 🙂
No problem.
Just amazing enhances the performance ..! Very thanks ..!
Thank you for this little code
No problem.
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!
Just put the code after the # End WordPress:)
Thank you for this information. Btw, what captcha that you used for this comment captcha?
It’s “SI CAPTCHA Anti-Spam“
If I have RewriteCond after my # END WordPress, do I put the above code before or after the multiple RewriteCond I have?
You can put either before or after but you have to put the code once (Don’t place it twice).
Much needed code who have wordpress powered website.
thanks for sharing
Yep any time. 🙂
Wow!! Thanks for this code, my PageSpeed Score went from 74% to 92%!
No problem bro 🙂
This is it!!! Wow!
GTmetrix: Got from PageSpeed Score 75 to 90!
YSlow score: From 91 to 92.
Thanks a lot!
Yep. That’s it. I know it is amazing. Thanks for stopping by.
You nailed it,
Thank you
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!
Hi Haroon,
I am facing 500 internal server error after pasting .htaccess code.
Can you please guide me.
That’s normal in WordPress when you edit htaccess in a wrong way. just check this out for original htaccess file of WordPress.
Hello I tried this and it didn’t work GTMetrix still scores it 0. What else can I do
Contact Web hosting to increase privileges
Hi,
I added into .htaccess file . but it’s not working. can you please provide another way or something
thanks
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!
Then you should consult with your host because they might blocking stagging environment of the secondary language.
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
Well, ask your web hosting provider to enabled the feature because in many cases the hosting providers have this feature disabled in apache configuration.
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.
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. 🙂
You are more than welcome.
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.
sir very good artical and thanks for this post.
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.